使用Azure B2C与MVC应用程序进入无限循环导致错误请求 - 请求太长Http 400错误

时间:2017-12-01 17:42:23

标签: azure azure-ad-b2c

因此,我构建并发布了一个使用Azure B2C作为身份验证机制的新网站。

我发现登录和标志可以正常工作一段时间。但是经过一段时间,比如在访问网站部署后的几个小时后,我会发现在登录或注册后,在成功进行身份验证后,我的浏览器将不会被重定向回到b​​2c配置中设置的返回URL。在最终完成Http 400错误消息并显示消息 - 错误请求 - 请求太长之前,在使用authorize属性保护的后验证登录页面和Azure B2C登录页面之间陷入无限循环之间。

我做了一些谷歌搜索,有很多帖子表明问题出在cookie上,删除cookie应解决问题。不是这种情况。我发现解决这个问题的唯一办法是重新启动网络服务器上的应用程序,或等待24小时重置某种缓存或应用程序池。任何人都有什么想法在这里发生什么?

1 个答案:

答案 0 :(得分:1)

好的,我想我可能找到了答案。

看起来Microsoft.Owin库存在问题以及设置Cookie的方式。直接写入System.Web可以根据this article解决这个问题。

有三种建议的解决方案:

  1. 确保在身份验证之前建立会话:System.Web和Katana Cookie之间的冲突是按请求进行的,因此应用程序可能会在身份验证流程之前根据某些请求建立会话。当用户第一次到达时,这应该很容易,但是当会话或授权cookie过期和/或需要刷新时,可能更难保证。

  2. 禁用SessionStateModule:如果应用程序不依赖于会话信息,但会话模块仍在设置导致上述冲突的cookie,那么您可以考虑禁用会话状态模块。

    < / LI>
  3. 重新配置CookieAuthenticationMiddleware以直接写入System.Web的cookie集合。

  4. 我将选择第三个选项,即覆盖默认的Cookie AuthenticationMiddleware,如下所示。

    app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                // ...
                CookieManager = new SystemWebCookieManager()
            });
    
    public class SystemWebCookieManager : ICookieManager
    {
        public string GetRequestCookie(IOwinContext context, string key)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
    
            var webContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
            var cookie = webContext.Request.Cookies[key];
            return cookie == null ? null : cookie.Value;
        }
    
        public void AppendResponseCookie(IOwinContext context, string key, string value, CookieOptions options)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
    
            var webContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
    
            bool domainHasValue = !string.IsNullOrEmpty(options.Domain);
            bool pathHasValue = !string.IsNullOrEmpty(options.Path);
            bool expiresHasValue = options.Expires.HasValue;
    
            var cookie = new HttpCookie(key, value);
            if (domainHasValue)
            {
                cookie.Domain = options.Domain;
            }
            if (pathHasValue)
            {
                cookie.Path = options.Path;
            }
            if (expiresHasValue)
            {
                cookie.Expires = options.Expires.Value;
            }
            if (options.Secure)
            {
                cookie.Secure = true;
            }
            if (options.HttpOnly)
            {
                cookie.HttpOnly = true;
            }
    
            webContext.Response.AppendCookie(cookie);
        }
    
        public void DeleteCookie(IOwinContext context, string key, CookieOptions options)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
    
            AppendResponseCookie(
                context,
                key,
                string.Empty,
                new CookieOptions
                {
                    Path = options.Path,
                    Domain = options.Domain,
                    Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                });
        }
    }
    

    我会给出一个破解,并将我的结果发回这里。