ASP.. NET MVC基本身份验证与Android 4.0 Chrome

本文关键字:Android Chrome 身份验证 NET MVC ASP | 更新日期: 2023-09-27 18:16:58

我已经在我的MVC网站上设置了一个基本的认证ActionFilterAttribute,在开发过程中锁定它,它在我测试的所有浏览器(IE9+, Chrome, FF, iOS Safari)上100%有效,但是当我在Android 4.0中加载Chrome时,它只是显示一个401访问拒绝,并且从不要求我提供基本的认证凭据?

这是我的OnAuthorization方法代码:

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var controllerName = (filterContext.RouteData.Values["controller"] as string).ToLower();
        if (_controllersToIgnore.Contains(controllerName))
        {
            return;
        }
        bool credentialsMatch = false;
        var req = filterContext.HttpContext.Request;
        if (!string.IsNullOrEmpty(req.Headers["Authorization"]))
        {
            var cred = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(req.Headers["Authorization"].Substring(6))).Split(':');
            var user = new { Name = cred[0], Pass = cred[1] };
            if (user.Name == Username && user.Pass == Password)
            {
                credentialsMatch = true;
            }
        }
        if (!credentialsMatch)
        {
            var res = filterContext.HttpContext.Response;
            res.StatusCode = 401;
            res.AddHeader("WWW-Authenticate", "Basic realm='"'"");
            res.End();
            filterContext.Result = new EmptyResult();
        }
    }

ASP.. NET MVC基本身份验证与Android 4.0 Chrome

这个问题自己消失了,奇怪的是,我什么也没做(显然),它就停止发生了。