Azure AD身份验证无限循环,错误请求过多

时间:2020-01-11 19:14:53

标签: asp.net azure authentication webforms azure-active-directory

我在ASP.Net中构建了一个Web Forms应用程序,并且正在使用Azure AD身份验证,以便用户可以使用其Microsoft凭据登录并进行正确的身份验证。

我密切关注了Microsoft提供的以下教程: https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-asp-webapp

该应用程序在大多数情况下都可以正常运行,除了一个烦人的问题。用户有时会收到以下错误消息“ ERR_TOO_MANY_REDIRECTS”“尝试清除您的cookie。”清除cookie并重新登录确实可以解决问题,但是显然这并不理想。

问题是我自己无法重现该问题,因此很难确定到底是什么原因造成的。

我有一个母版页,并且在其背后的代码中,我正在运行以下代码,以确保在浏览应用程序时对用户进行身份验证

protected void Page_Load(object sender, EventArgs e)
        {
            //Check if user is authenticated on each page request
            if (!Request.IsAuthenticated)
            {
                HttpContext.Current.GetOwinContext().Authentication.Challenge(
                    new AuthenticationProperties { RedirectUri = "/home.aspx" },
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }
            else
            {
                try
                {
                    //Check if connection string is set
                    if (Session["ConnectionString"].ToString() == "")
                    {
                        Response.Redirect("home.aspx"); //If empty redirect user to Home page to set connection string
                    }

                    //Get data for current user and display their username and email address
                    ClaimsPrincipal cp = ClaimsPrincipal.Current;
                    userName.InnerText = cp.FindFirst(ClaimTypes.GivenName).Value + " " + cp.FindFirst(ClaimTypes.Surname).Value;
                    email.InnerText = HttpContext.Current.User.Identity.Name;
                } 
                catch(Exception ex)
                {

                }
            }  
        }

在WebConfig文件中,我将“重定向URL”设置为

<add key="RedirectUrl" value="https://mywebsite.com/auth/openid/return" />

在Azure门户中,我使用了相同的RedirectUrl

您知道什么可能导致用户偶尔遇到无限循环的问题吗?

0 个答案:

没有答案