开发者

Connect To Secure WebService

开发者 https://www.devze.com 2023-03-07 01:47 出处:网络
I want to connect web service (as service reference) which is secured by certificate. for this I\'m doing this:

I want to connect web service (as service reference) which is secured by certificate.

for this I'm doing this:

    ServiceClient service = new ServiceClient();
    service.ClientCredentials.ClientCertificate.SetCertificate (StoreLocation.CurrentUser, StoreName.Root, X509FindType.FindBySubjectName, "Subject Name");
    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(Validati开发者_StackOverflow中文版onCallBack);
    service.SomeMethod();

    private bool ValidationCallBack(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
    {
         return true;
    }

When I call service.SomeMethod(); method, it throws an exception:

"The HTTP request was forbidden with client authentication scheme 'Anonymous'."

What does this error means? And how can I avoid this to call web service method?

Thanks!


You're doing the right things to connect via SSL (I assuming your server requires client certificates, usually not required), but it doesn't look like you're passing any credentials to the web service.

Try adding this:

service.Credentials = System.Net.CredentialCache.DefaultCredentials;

This will pass the credentials of the currently logged on user to the service.

As long as the current user has permissions to access the service, it should let you in. If the user with permissions on the service is different than the currently logged in user:

credentialCache cache = new CredentialCache();
cache.Add( new Uri(service.Url), // Web service URL
       "Negotiate",  // Kerberos or NTLM
       new NetworkCredential("username", "password", "domainname") );
service.Credentials = cache;

Things can get a bit more complicated if you're using a proxy.

http://man.ddvip.com/web/bsaspnetapp/LiB0087.html seemed like an ok writeup if you need more information.


From my feeble experience, which isnt alot, it means that your service.svc is not open to anonymous, which, frustrated me, as all my site was NTLM protected, but, for some reason your service file needs to be anonymous

0

精彩评论

暂无评论...
验证码 换一张
取 消