c# – AspNetCore.Identity中的Google身份验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – AspNetCore.Identity中的Google身份验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2081字,纯文字阅读大概需要3分钟。
内容图文
开始玩AspNetCore.Identity,但不能运行简单的例子,总是收到这样的异常:
An unhandled exception occurred while processing the request.
InvalidOperationException: No authentication handler is configured to
handle the scheme: google
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// EF services
services.AddEntityFramework()
.AddEntityFrameworkSqlServer()
.AddDbContext<MyContext>();
// Identity services
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
.AddDefaultTokenProviders();
// MVC services
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters = new JsonConverter[] { new StringEnumConverter(), new IsoDateTimeConverter() };
});
Configure.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentity();
app.UseCookieAuthentication();
app.UseGoogleAuthentication(new GoogleOptions()
{
ClientId = "xxx",
ClientSecret = "xxx",
AutomaticChallenge = true,
AutomaticAuthenticate = true
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}");
});
AuthController.cs
[HttpGet]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Auth", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
返回ChallengeResult后发生异常.
我错过了什么?
解决方法:
您正在使用app.UseIdentity()并将Google中间件上的AutomaticAuthenticate = true设置为true. Identity将cookie auth设置为AutomaticAuthenticate,并且您只能将单个身份验证中间件设置为自动,否则行为未定义.
您将在documentation中看到,当Facebook连接时,它未设置为自动进行身份验证.
内容总结
以上是互联网集市为您收集整理的c# – AspNetCore.Identity中的Google身份验证全部内容,希望文章能够帮你解决c# – AspNetCore.Identity中的Google身份验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。