使用c#访问安全的Web服务

本文关键字:Web 服务 安全 访问 使用 | 更新日期: 2023-09-27 18:01:45

我必须使用安全的(https) web服务,这需要域用户凭据。我有以下代码来访问该服务。

    public string GetResponse()
    {
        var web = new ProxyStatisticsWebServiceSoapClient();
        web.ClientCredentials.Windows.AllowNtlm = true;
        web.ClientCredentials.Windows.ClientCredential = new NetworkCredential("username",
            "password", "domail");
        var result  = web.QueryProxyStatistics();
        return result;
    }

每当我调用web.QueryProxyStatistics();方法时,我得到MessageSecurityException异常。HTTP请求未经授权,客户端身份验证方案为"匿名"。从服务器收到的身份验证头是'NTLM'.)代码有什么问题吗?

使用c#访问安全的Web服务

尝试使用以下代码:

web.ClientCredentials.Windows.AllowedImpersonationLevel =  System.Security.Principal.TokenImpersonationLevel.Impersonation;