예제 #1
0
        private string CalculateClientResponse(AuthorizationHeader header, string password)
        {
            var a1Hash   = $"{header.Username}:{header.Realm}:{password}".ToMd5Hash();
            var a2Hash   = $"{header.RequestMethod}:{header.Uri}".ToMd5Hash();
            var response = $"{a1Hash}:{header.Nonce}:{header.NonceCounter}:{header.ClientNonce}:{header.Qop}:{a2Hash}"
                           .ToMd5Hash();

            return(response);
        }
예제 #2
0
        private AuthorizationHeader GetAuthenticationHeader(HttpRequest request)
        {
            AuthorizationHeader authHeader = new AuthorizationHeader();

            try
            {
                string credentials = GetCredentials(request);
                if (string.IsNullOrEmpty(credentials))
                {
                    return(authHeader);
                }

                authHeader.RequestMethod = request.Method;
                var nameValueStrs = credentials
                                    .Replace("\"", string.Empty)
                                    .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                    .Select(s => s.Trim());

                foreach (var nameValueStr in nameValueStrs)
                {
                    var index = nameValueStr.IndexOf('=');
                    var name  = nameValueStr.Substring(0, index);
                    var value = nameValueStr.Substring(index + 1);

                    switch (name)
                    {
                    case "username":
                        authHeader.Username = value;
                        break;

                    case "realm":
                        authHeader.Realm = value;
                        break;

                    case "nonce":
                        authHeader.Nonce = value;
                        break;

                    case "cnonce":
                        authHeader.ClientNonce = value;
                        break;

                    case "nc":
                        authHeader.NonceCounter = value;
                        break;

                    case "qop":
                        authHeader.Qop = value;
                        break;

                    case "response":
                        authHeader.Response = value;
                        break;

                    case "uri":
                        authHeader.Uri = value;
                        break;
                    }
                }

                return(authHeader);
            }
            catch (Exception e)
            {
                return(authHeader);
            }
        }