天天看点

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

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

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

我的解决方法:

1,使用http的endpoint:

<security mode="transportcredentialonly">

2,使用https的endpoint:

<security mode="transport">

粘贴出client端的app.config

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

<?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" />

<transport clientcredentialtype="ntlm" proxycredentialtype="none"

realm="">

<extendedprotectionpolicy policyenforcement="never" />

</transport>

<message clientcredentialtype="username" algorithmsuite="default" />

</security>

</binding>

<binding name="basichttpbinding_businessdatacatalogsharedservice1"

<!--<extendedprotectionpolicy policyenforcement="never" />-->

</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>

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

client端的代码如下:

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

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);

/// 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.

/// <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;

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

继续阅读