简体   繁体   English

AspNetCore.Identity中的Google身份验证

[英]Google authentication in AspNetCore.Identity

Starting to play around with AspNetCore.Identity, but cannot run simple example, always receive such exception: 开始玩AspNetCore.Identity,但不能运行简单的例子,总是收到这样的异常:

An unhandled exception occurred while processing the request. 处理请求时发生未处理的异常。 InvalidOperationException: No authentication handler is configured to handle the scheme: google InvalidOperationException:没有配置身份验证处理程序来处理该方案:google

Startup.cs 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 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 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);
    }

Exception happened somewhere after ChallengeResult is returned. 返回ChallengeResult后发生异常。 Did I miss something? 我错过了什么?

You're using both app.UseIdentity() and setting AutomaticAuthenticate = true on your google middleware to true. 您正在使用app.UseIdentity()并将Google中间件上的AutomaticAuthenticate = true设置为true。 Identity sets cookie auth to AutomaticAuthenticate, and you can only have a single authentication middleware set as automatic, otherwise the behaviour is undefined. Identity将cookie auth设置为AutomaticAuthenticate,并且您只能将单个身份验证中间件设置为自动,否则行为未定义。

You'll see in the documentation that when facebook is wired up it is not set to automatically authenticate. 您将在文档中看到,当Facebook连接时,它设置为自动进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 AddPooledDbContextFactory 与 AspNetCore.Identity 一起使用 - Using AddPooledDbContextFactory with AspNetCore.Identity 覆盖AspNetCore.Identity表中的默认索引 - Override default indexes in AspNetCore.Identity tables AspNetCore.Identity LockoutOptions.AllowedForNewUsers属性 - AspNetCore.Identity LockoutOptions.AllowedForNewUsers Property ViewComponent 无法识别 AspNetCore.Identity“用户” - ViewComponent does not recognize AspNetCore.Identity "User" 身份服务器 4 + AspNetCore.Identity:X 尝试后如何锁定? - identity server 4 + AspNetCore.Identity: how to lockout after X tries? AspNetCore.Identity 不适用于自定义用户/角色实现 - AspNetCore.Identity not working with custom User/Role implementation AspNetCore.Identity - 如何为新用户设置 LockoutEnabled = false - AspNetCore.Identity - How to set LockoutEnabled = false for a new user ASP.NET核心 - 自定义AspNetCore.Identity实现不起作用 - ASP.NET Core - custom AspNetCore.Identity implementation not working 如何在 AspNetCore.Identity 上删除基本字段并添加自定义字段? - How to delete basic fields and add custom fields on AspNetCore.Identity? 如何使用 AspNetCore.Identity 为多个用户获取声明? - How to get Claims for multiple users with AspNetCore.Identity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM