Azure AD从MVC应用程序登录时出现重定向循环

3

我已经阅读了许多帖子,所有的帖子都归结于 Cookie 认证时出现的问题。然而,当我在登录后直接遇到重定向循环时,它会影响到任何登录的客户端,而不仅仅是特定的计算机。

我已经尝试了一切可能的方法,并可以发布我所做的一切,但我看不出这如何是一个 Cookie 问题,如果所有用户都发生这种情况。所以有时它能够工作,下一刻就由于重定向循环而没有人可以登录。

逻辑上来说,这一定与认证 Cookie 有关,但它如何会同时影响所有人呢?

这难道不是 Azure 中可能导致问题的某些因素吗?似乎必须是这样的吧?

如有任何想法将不胜感激,因为我现在从我的客户那里收到了很多抱怨 :(

如果可以的话,以下是我的 startup.auth 内容:

private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
        private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
        private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
        private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];


        // Concatenate aadInstance, tenant to form authority value       
        private string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

        // ConfigureAuth method  
        public void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            //app.UseCookieAuthentication(new CookieAuthenticationOptions());

            //Enable the application to use a cookie to store information for the signed in user

            //and to use a cookie to temporarily store information about a user logging in with a third party login provider

            //Configure the sign in cookie


            //app.UseCookieAuthentication(new CookieAuthenticationOptions
            //{
            //    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            //    LoginPath = new PathString("/Account/Login"),
            //    Provider = new CookieAuthenticationProvider
            //    {
            //        // Enables the application to validate the security stamp when the user logs in.
            //        // This is a security feature which is used when you change a password or add an external login to your account.  
            //        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            //            validateInterval: TimeSpan.FromMinutes(30),
            //            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            //    },
            //    CookieSecure = CookieSecureOption.Always
            //});

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                CookieName = "Local_Login",
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                            validateInterval: TimeSpan.FromMinutes(30),
                            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },
                //CookieManager = new SystemWebCookieManager(),
                SlidingExpiration = true
            });

            //app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(

                            new OpenIdConnectAuthenticationOptions
                            {
                                ClientId = clientId,
                                Authority = authority,
                                RedirectUri = redirectUri,
                                PostLogoutRedirectUri = postLogoutRedirectUri,
                                Notifications = new OpenIdConnectAuthenticationNotifications
                                {
                                    AuthenticationFailed = (context) =>
                                    {
                                        context.HandleResponse();
                                        context.OwinContext.Response.Redirect("/Account/Login");
                                        return Task.FromResult(0);
                                    }
                                }
                            });


        } // end - ConfigureAuth method  

通常在这种情况下有帮助的是在受影响的机器上启用开发者控制台,并为控制台和网络选项卡启用“保留日志”。可能是重定向URI出了一些奇怪的问题,但是由于信息太少,很难确定。 - Alex AIT
如果您重新启动应用程序,循环问题是否暂时消失? - Tiago B
Alex - 当重定向循环问题再次发生时,我一定会尝试解决。TiagoBrenck,是的,在重新启动应用程序服务后,重定向循环问题暂时消失了。当我再次发布工具时,它也会暂时消失。因此,我无法理解这可能是一个cookie问题,因为这将单独影响特定的浏览器,或者我错了吗? - AxleWack
1个回答

4
经过许多个小时的努力和熬夜,我终于似乎找到了解决方案。
据我的理解,在某一点上,“MVC” cookie(我认为是application.cookie)和Azure AD cookie(我认为是aspnet.cookie)会相互删除,这是由于微软内部的一个错误,在多年来没有解决该问题。所以现在发生的情况是,你已经登录了Azure AD并且auth cookie说你已经通过验证,但当进入控制器时,“MVC” cookie(如文章中所述)说你没有通过验证,因此重定向回Azure AD登录,但看到你已经通过Azure AD进行了验证,所以无限循环继续下去。
我看到许多人提到了Kentor Cookie Saver并表示它有帮助,也有一两个人说它没有用,但我决定尝试一下,最近2-3天客户没有抱怨,所以它肯定起作用了。
这是我使用的链接:Kentor Cookie Saver 希望这能帮到某些人或引导他们走向正确的方向。
干杯!

这个修复方案解决了你的问题吗?你找到“它如何同时影响每个人”的答案了吗?因为我也遇到了同样的问题。 - Cozdemir
是的,我已经解决了,我使用了Kentor Cookie Saver - 我在我的帖子中添加了链接。 - AxleWack
谢谢。我只是不明白Cookie问题如何同时影响所有客户端。 - Cozdemir
我猜它更多地影响每个人,无论是1个用户还是100个用户尝试登录,因此“同时所有人”这个措辞应该改为“任何尝试登录的人都会受到影响”。 - AxleWack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接