gpt4 book ai didi

c# - AspNetCore.Identity 中的谷歌身份验证

转载 作者:太空狗 更新时间:2023-10-30 01:15:17 25 4
gpt4 key购买 nike

开始玩弄AspNetCore.Identity,但是无法运行简单的例子,总是收到这样的异常:

An unhandled exception occurred while processing the request. InvalidOperationException: No authentication handler is configured to handle the scheme: google

启动.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() };
});

配置.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 中的谷歌身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38920223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com