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

标签 c# asp.net-core asp.net-identity asp.net-core-mvc .net-core

开始玩弄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/

相关文章:

c# - 使用 ASP.NET Identity 实现权限

c# - 使用相同形式的 .net 身份注册多个用户

c# - 通用类的通用集合?

c# - 运行空间 PowerShell 输出

c# - 根据 DTO、实体模型或其他东西验证服务层中的数据?

c# - 如何将在 TextBox 控件中输入的 long 拆分为 int 数组

mysql - 在 ASP.NET Core 2.0 预览版中使用 Pomelo MySQL EF 提供程序时出错

asp.net-core - AspNetCore 使用OpenIdConnectAuthentication

asp.net-core - 如何使用 ReSharper 为 DNX 项目运行 xUnit 2.1.0-beta-*

.net-core - 我们可以保护仅使用 aspnet 身份的 dotnet core 2.0 React App 吗?