在AspNetCore.Identity中的Google身份验证
本文关键字:Google 身份验证 中的 Identity AspNetCore | 更新日期: 2023-09-27 18:02:21
开始使用AspNetCore。身份,但不能运行简单的例子,总是收到这样的异常:
处理请求时发生未处理的异常。InvalidOperationException:没有配置身份验证处理程序处理方案: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()
并将AutomaticAuthenticate = true
设置为true。Identity将cookie认证设置为AutomaticAuthenticate,并且您只能将单个身份验证中间件设置为automatic,否则行为未定义。
您将在文档中看到,当facebook连接时,它是而不是设置为自动身份验证