[英]Using AddPooledDbContextFactory with AspNetCore.Identity
[英]Google authentication in AspNetCore.Identity
开始玩AspNetCore.Identity,但不能运行简单的例子,总是收到这样的异常:
处理请求时发生未处理的异常。 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()
并将Google中间件上的AutomaticAuthenticate = true
设置为true。 Identity将cookie auth设置为AutomaticAuthenticate,并且您只能将单个身份验证中间件设置为自动,否则行为未定义。
您将在文档中看到,当Facebook连接时,它未设置为自动进行身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.