簡體   English   中英

AspNetCore.Identity中的Google身份驗證

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM