web service client authentication

Posted by Jack on Stack Overflow See other posts from Stack Overflow or by Jack
Published on 2010-06-02T08:27:27Z Indexed on 2010/06/02 8:33 UTC
Read the original article Hit count: 243

Filed under:
|
|
|

I want to consume Java based web service with c#.net client.

The problem is, I couldnt authenticate to the service.

it didnt work with this:

mywebservice.Credentials = new System.Net.NetworkCredential(userid, userpass);

I tried to write base class for my client method.

public class ClientProtocols : SoapHttpClientProtocol
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            System.Net.WebRequest request = base.GetWebRequest(uri);
            if (null != Credentials)
                request.Headers.Add("Authorization", GetAuthHeader());
            return request;
        }
        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            return response;
        }
        private string GetAuthHeader()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Basic ");
            NetworkCredential cred = Credentials.GetCredential(new Uri(Url), "Basic");
            string s = string.Format("{0}:{1}", cred.UserName, cred.Password);
            sb.Append(Convert.ToBase64String(Encoding.ASCII.GetBytes(s)));
            return sb.ToString();
        }
    }

How can I use this class and authorize to the web service?

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET