Google AspNetCore.Identity 中的身份验证
Google authentication in AspNetCore.Identity
开始玩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,并且只能将单个身份验证中间件设置为automatic,否则行为未定义。
您会在 documentation 中看到,当 facebook 连接时,未 设置为自动验证。
开始玩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,并且只能将单个身份验证中间件设置为automatic,否则行为未定义。
您会在 documentation 中看到,当 facebook 连接时,未 设置为自动验证。