簡體   English   中英

使用 Google 和 ASP.NET 核心身份的無限登錄重定向循環

[英]Infinite login redirect loop with Google and ASP.NET Core Identity

在嘗試訪問 Net Core 3 中 Authorize 屬性后面的頁面時,無論我是否已登錄或用戶是否具有權限,我都遇到了重定向循環。 我已經按照 MSDN 教程進行了一些搜索,但還沒有找到解決我的問題的方法。 我懷疑它沒有識別我的身份 cookie,或者它沒有重定向到正確的頁面,但我需要一些關於查找內容的指導。

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<GuitarCatalogMVCContext>(options =>
                options.UseSqlite( Configuration.GetConnectionString("DefaultConnection")));


            services.AddIdentity<GuitarCatalogAuthUser, IdentityRole>().AddEntityFrameworkStores<GuitarCatalogMVCContext>().AddDefaultTokenProviders();

            services.AddControllersWithViews();
            services.AddRazorPages();

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddGoogle(options =>
            {
                IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");

                options.ClientId = googleAuthNSection["ClientId"];
                options.ClientSecret = googleAuthNSection["ClientSecret"];
                options.CallbackPath = new PathString("/signin-google");
            });

            services.AddAuthorization();
        }

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();


            app.UseRouting();

            app.UseAuthorization();
            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }

/Identity/Account/Login使用 Google 登錄,我得到了正確的提示,我可以成功登錄,並且 url 是:

https://accounts.google.com/signin/oauth/oauthchooseaccount? 
response_type=code &client_id= ______ 
&redirect_uri=https%3A%2F%2Flocalhost%3A44346%2Fsignin-google 
&scope=openid%20profile%20email &state= _______ &flowName=GeneralOAuthFlow

當我打開[Authorize]屬性后面的 controller 方法時,通過<a href="@Url.Action("Create", "Listings")" target="_blank"><h1>TEST CREATE LISTING</h1></a> ,它讓我登錄,然后立即將我帶回同一頁面

我在開發工具中打開了網絡選項卡,在這里我可以看到我的登錄在哪里處理,它把我帶到了創建頁面,然后我最終回到了登錄頁面。 我沒有看到任何表明我的登錄失敗(或者為什么在我登錄時提示我再次登錄)。

在此處輸入圖像描述

似乎問題出在中間件執行順序上。

app.UseAuthorization()應該在app.UseAuthentication()之后執行

在您當前的實現中,因為首先執行授權中間件,用戶 object 從未設置,因此所有請求似乎都是未經授權的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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