开始玩AspNetCore.Identity,但无法运行简单示例,总是会收到这样的异常:
处理请求时发生未处理的异常。 InvalidOperationException:未配置身份验证处理程序 处理方案:谷歌
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
之后的某个地方发生了异常。
我错过了什么吗?
答案 0 :(得分:4)
您正在使用app.UseIdentity()
并在Google中间件上设置AutomaticAuthenticate = true
为true。 Identity将cookie auth设置为AutomaticAuthenticate,并且您只能将一个身份验证中间件设置为自动,否则行为未定义。
您会在documentation中看到,当Facebook连线时,不设置为自动进行身份验证。