1

I have a webservice which has a method to return a value.Here my requirement is , I want to pass the authentication value in header for each and every web request i used the below given code for doing the same

req = WebRequest.Create(http://localhost/test/test.asmx); string _auth = string.Format("{0}:{1}", "username", "password"); string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth)); string _cred = string.Format("{0} {1}", "Basic", _enc); req.Headers[HttpRequestHeader.Authorization] = _cred; WebResponse response = (WebResponse)req.GetResponse(); 

But through this way I am not able to call the webmethod. Is there any way to achieve the same.

Also I tried to sending the authentication values with the help of ICredentials with the following given code , but i need the header values like "Authorization: Basic YWRtaW46YWRtaW4="

 ICredentials creds; creds = new NetworkCredential("username", "password", "domain"); proxy.Credentials = creds; string str=proxy.helloworld(); 

please let me know if there is any way to achieve this..any help is appreciated.

thanks

1 Answer 1

2

Copying from this post.

protected override System.Net.WebRequest GetWebRequest(Uri uri) { HttpWebRequest request; request = (HttpWebRequest)base.GetWebRequest(uri); if (PreAuthenticate) { NetworkCredential networkCredentials = Credentials.GetCredential(uri, “Basic”); if (networkCredentials != null) { byte[] credentialBuffer = new UTF8Encoding().GetBytes( networkCredentials.UserName + “:” + networkCredentials.Password); request.Headers["Authorization"] = “Basic” + Convert.ToBase64String(credentialBuffer); } else { throw new ApplicationException(“No network credentials”); } } return request; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.