代码改变世界

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

2010-06-29 14:12  轩脉刃  阅读(3416)  评论(0编辑  收藏  举报

当使用VS2008 作为client call sharepoint的service(WCF)的时候显示异常:

 HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'

 

我的解决方法:

1,使用http的endpoint:

<security mode="TransportCredentialOnly">

 

2,使用https的endpoint:

<security mode="Transport">

 

粘贴出client端的app.config

 

代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_BusinessDataCatalogSharedService"
closeTimeout
="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout
="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode
="StrongWildcard" maxBufferSize="999999"
maxBufferPoolSize
="9999999" maxReceivedMessageSize="999999"
messageEncoding
="Mtom" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy
="true">
<readerQuotas maxDepth="99" maxStringContentLength="999999" maxArrayLength="999999"
maxBytesPerRead
="999999" maxNameTableCharCount="999999" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm
="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="BasicHttpBinding_BusinessDataCatalogSharedService1"
closeTimeout
="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
sendTimeout
="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode
="StrongWildcard" maxBufferSize="999999"
maxBufferPoolSize
="9999999" maxReceivedMessageSize="999999"
messageEncoding
="Mtom" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy
="true">
<readerQuotas maxDepth="99" maxStringContentLength="999999" maxArrayLength="999999"
maxBytesPerRead
="999999" maxNameTableCharCount="999999" />
<security mode="Transport">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm
="">
<!--<extendedProtectionPolicy policyEnforcement="Never" />-->
</transport>
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://SUT02/_vti_bin/BdcAdminService.svc"
binding
="basicHttpBinding" bindingConfiguration="BasicHttpBinding_BusinessDataCatalogSharedService"
contract
="BusinessDataCatalogSharedService" name="BasicHttpBinding_BusinessDataCatalogSharedService" />
<endpoint address="https://SUT02:443/_vti_bin/BdcAdminService.svc"
binding
="basicHttpBinding" bindingConfiguration="BasicHttpBinding_BusinessDataCatalogSharedService1"
contract
="BusinessDataCatalogSharedService" name="BasicHttpBinding_BusinessDataCatalogSharedService1" />
</client>
</system.serviceModel>
</configuration>

 

client端的代码如下:

 

代码
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;
}