简体   繁体   中英

web service client authentication

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? Where do I put this class in solution?

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM