【问题标题】:Google Authentication Exception- No authentication handler is configured to handle the scheme: CookiesGoogle 身份验证异常 - 未配置身份验证处理程序来处理该方案:Cookies
【发布时间】:2017-04-25 10:00:44
【问题描述】:

我在 .net 核心中使用 google 身份验证,但是当从 google 重定向时,它显示异常- InvalidOperationException:没有配置身份验证处理程序来处理该方案:Cookies

这是我的 startup.cs 设置

  app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            LoginPath = new PathString("/account/login"),
            AuthenticationScheme = "MyCookieMiddlewareInstance",
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            AccessDeniedPath = new PathString("/Home/AccessDenied"),
        });

        app.UseGoogleAuthentication(new GoogleOptions
        {
            AuthenticationScheme = "Google",
            DisplayName = "Google",
            SignInScheme = "Cookies",
            ClientId = "ClientId ",
            ClientSecret = "ClientSecret ",
            Scope = { "email", "openid" },
            CallbackPath = "/home",
        });

请指出我哪里错了

【问题讨论】:

    标签: c# authentication asp.net-core google-authentication


    【解决方案1】:

    您可以使用AddCookieAuthentication扩展方法注册ASP.NET Core提供的默认CookieAuthenticationHandler处理程序:

    using Microsoft.AspNetCore.Authentication.Cookies;
    

    public void ConfigureServices(IServiceCollection 服务) { services.AddCookieAuthentication(); ... }

    更新

    身份验证中间件中有一个breaking change。现在 ASP.NET Core 只有一个 AddAuthentication() 扩展方法,所有配置都会抛出相应的 UseXXXAuthentication 扩展方法。举个简单的例子看看Security repo samples:

    using Microsoft.AspNetCore.Authentication.Cookies;
    
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication();
    }
    
    public void Configure(IApplicationBuilder app)
    {
         ...
         app.UseCookieAuthentication(new CookieAuthenticationOptions
         {
            AutomaticAuthenticate = true
         });
    }
    

    查看文档中的authentication-cookie 部分以获取可用选项

    【讨论】:

    • AddCookieAuthentication 不是 IServiceCollection 对象的方法。我确认引用了这个命名空间,但仍然得到这个异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多