您的位置:首页 > 理论基础 > 计算机网络

HTTP request is unauthorized with client authentication scheme 'Anonymous'.

2010-06-29 14:12 1046 查看
当使用VS2008 作为client call sharepoint的service(WCF)的时候显示异常:

HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'[b]代码

static void Main(string[] args)
{
BusinessDataCatalogSharedServiceClient client = new BusinessDataCatalogSharedServiceClient("BasicHttpBinding_BusinessDataCatalogSharedService1");
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ClientCredentials.UserName.UserName = @"domain\userName";
client.ClientCredentials.UserName.Password = "Password";
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("username", "Password", "domain");
AcceptAllCertificate();
try
{
Guid guid = client.GetServiceApplicationId();
}
catch (Exception ex)
{
throw;
}

}

/// <summary>
/// Case request Url include HTTPS and TCP prefix, use this function to avoid closing base connection.
/// Local client will accept all certificate after execute this function.
/// </summary>
public static void AcceptAllCertificate()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
}
/// <summary>
/// Verifies the remote Secure Sockets Layer (SSL) certificate used for authentication.
/// In our adapter,we make this method always return true, make client can communicate with server under HTTPS without a certification.
/// </summary>
/// <param name="sender">An object that contains state information for this validation.</param>
/// <param name="certificate">The certificate used to authenticate the remote party.</param>
/// <param name="chain">The chain of certificate authorities associated with the remote certificate.</param>
/// <param name="sslPolicyErrors">One or more errors associated with the remote certificate.</param>
/// <returns>A Boolean value that determines whether the specified certificate is accepted for authentication.</returns>
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐