【问题标题】:Basic authentication not working in IE or Chrome but work on Firefox基本身份验证不适用于 IE 或 Chrome,但适用于 Firefox
【发布时间】:2014-06-02 06:43:02
【问题描述】:

我的身份验证代码如下:

public class BasicAuthenticationMessageHandler : DelegatingHandler
    {
        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic";

        public IProvidePrincipal PrincipalProvider { get; set; }

        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            AuthenticationHeaderValue authValue = request.Headers.Authorization;
            if (authValue != null && !String.IsNullOrWhiteSpace(authValue.Parameter))
            {
                Credentials parsedCredentials = ParseAuthorizationHeader(authValue.Parameter);
                if (parsedCredentials != null)
                {
                    Thread.CurrentPrincipal = PrincipalProvider
                        .CreatePrincipal(parsedCredentials.Username, parsedCredentials.Password);
                }
            }
            return base.SendAsync(request, cancellationToken)
                .ContinueWith(task =>
                {
                    var response = task.Result;
                    if (response.StatusCode == HttpStatusCode.Unauthorized
                        && !response.Headers.Contains(BasicAuthResponseHeader))
                    {
                        response.Headers.Add(BasicAuthResponseHeader
                                             , BasicAuthResponseHeaderValue);
                    }
                    return response;
                });
        }

        private Credentials ParseAuthorizationHeader(string authHeader)
        {
            string[] credentials = Encoding.ASCII.GetString(Convert
                                                                .FromBase64String(authHeader))
                .Split(
                    new[] { ':' });
            if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0])
                || string.IsNullOrEmpty(credentials[1])) return null;
            return new Credentials()
            {
                Username = credentials[0],
                Password = credentials[1],
            };
        }
}

我使用 Web API 作为后端,使用 Angularjs 作为前端。当我想调用一个需要经过身份验证的用户的操作时,在 Firefox 中我可以做到这一点,但在 IE 和 chrome 中我没有。 是什么原因?我认为这与 MediaTypeFormatter 有关。是真的吗?

【问题讨论】:

    标签: javascript json angularjs google-chrome firefox


    【解决方案1】:

    尝试将领域添加到您的 WWW-Authenticate 标头字段(根据 https://www.rfc-editor.org/rfc/rfc1945#section-11)。如果没有这个,IE 似乎拒绝记住身份验证,虽然我不能说我在 Chrome 中遇到过这个问题。

    这将导致你的班级顶部看起来像这样......

    public class BasicAuthenticationMessageHandler : DelegatingHandler
    {
        private const string BasicAuthResponseHeader = "WWW-Authenticate";
        private const string BasicAuthResponseHeaderValue = "Basic realm=\"MyRealm\"";
        //...etc
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多