private HttpCredentialsHeaderValue GetAuthorizationHeader(DigestAuthParameters authParameters) { string nonceCounterString = string.Format("{0:x08}", authParameters.NonceCounter++); var authorizationHeader = new HttpCredentialsHeaderValue("Digest"); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("username", UserName)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("realm", authParameters.Realm)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("qop", "auth")); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("uri", authParameters.Uri)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("nonce", authParameters.Nonce)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("nc", nonceCounterString)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("cnonce", authParameters.Cnonce)); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("algorithm", authParameters.Algorithm)); if (!String.IsNullOrEmpty(authParameters.Opaque)) { authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("opaque", authParameters.Opaque)); } var a1 = string.Format("{0}:{1}:{2}", UserName, authParameters.Realm, Password); string a2 = string.Format("POST:{0}", authParameters.Uri); string ha1 = GetMD5HashBinHex(a1); string ha2 = GetMD5HashBinHex(a2); string a = string.Format("{0}:{1}:{4}:{2}:auth:{3}", ha1, authParameters.Nonce, authParameters.Cnonce, ha2, nonceCounterString); string responseVal = GetMD5HashBinHex(a); authorizationHeader.Parameters.Add(new HttpNameValueHeaderValue("response", responseVal)); return(authorizationHeader); }
private DigestAuthParameters InitDigestAuthParameters(HttpChallengeHeaderValue authHeader, HttpResponseMessage response) { var authParameters = new DigestAuthParameters(); foreach (var headerValue in authHeader.Parameters) { switch (headerValue.Name) { case "realm": authParameters.Realm = headerValue.Value; break; case "nonce": authParameters.Nonce = headerValue.Value; break; case "algorithm": authParameters.Algorithm = headerValue.Value; break; case "opaque": authParameters.Opaque = headerValue.Value; break; } } if (String.IsNullOrEmpty(authParameters.Algorithm)) { authParameters.Algorithm = "MD5"; } authParameters.Uri = response.RequestMessage.RequestUri.AbsolutePath; authParameters.Cnonce = GetNonce(); return(authParameters); }