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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

轩脉刃 2016-05-17 09:59:03 浏览1585
展开阅读全文

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

 HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'NTLM'trans.gif?cver=2.15.0079.0

 

我的解决方法:

1,使用http的endpoint:

<security mode="TransportCredentialOnly">

 

2,使用https的endpoint:

<security mode="Transport">

 

粘贴出client端的app.config

 

ExpandedBlockStart.gif
复制代码

<?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端的代码如下:

 

ExpandedBlockStart.gif
复制代码

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

 

网友评论

登录后评论
0/500
评论