使用摘要式身份验证来使用Web服务

时间:2012-03-13 15:15:17

标签: c# web-services api digest-authentication

我们正在使用C#通过SOAP发送XML数据。该服务需要使用#PasswordDigest#Base64Binary Nonce进行HttpDigest身份验证。我们的binding代码:

protected BasicHttpBinding binding = new BasicHttpBinding()
{
            Name = "ShipmentServiceSoapBinding",
            CloseTimeout = new TimeSpan(0, 01, 0),
            OpenTimeout = new TimeSpan(0, 01, 0),
            ReceiveTimeout = new TimeSpan(0, 10, 0),
            SendTimeout = new TimeSpan(0, 5, 0),
            AllowCookies = false,
            BypassProxyOnLocal = false, 
            HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
            MaxBufferPoolSize = 5242880,
            MaxReceivedMessageSize = 655360,
            MessageEncoding = WSMessageEncoding.Text ,
            TextEncoding =  new UTF8Encoding(),
            UseDefaultWebProxy = true,
            ReaderQuotas = new XmlDictionaryReaderQuotas() { MaxDepth = 32, MaxStringContentLength = 81920, MaxArrayLength = 1638400, MaxBytesPerRead = 409600, MaxNameTableCharCount = 163840 },
            Security = new BasicHttpSecurity() { Mode = BasicHttpSecurityMode.TransportWithMessageCredential, 
                                                 //Message = new BasicHttpMessageSecurity() { AlgorithmSuite = SecurityAlgorithmSuite.Default, ClientCredentialType = BasicHttpMessageCredentialType.UserName}, 
                                                 Transport = new HttpTransportSecurity(){ ClientCredentialType = HttpClientCredentialType.Digest}},

};

根据我们选择的BasicHttpSecurityMode的类型,我们遇到了3个不同的问题。

  1. 传输 - XML不包含任何安全信息
  2. TransportCredentialOnly - 我们得到的错误表明端点不能是https://
  3. TransportWithMessagecredential - 这不是使用摘要
  4. 现在他们的ServiceReference允许我们使用ClientCredentials类,所以我们尝试使用HttpDigest:

    typeClient.ClientCredentials.HttpDigest.ClientCredential.UserName = "username";
    typeClient.ClientCredentials.HttpDigest.ClientCredential.Password = "password";
    

    我已经读过其他StackOverflow问题,对于摘要我们应该使用带有AuthHeader的SoapHeader,但是我们无法将它与它们在API中提供的内容相匹配。这样做还有其他办法吗?或者他们的API没有为C#正确编写?