How to pass Credential to Web Service with Basic Authentication
You can pass credential to web service in different ways
Ø Manual: Passing user Id and Password manually
Ø Default: Get the System Credential of the application.
Ø Network: Get the network Credential of the current security.
C#.NET code Sample for Manual
using System.Net;
// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
System.Net.CredentialCache userCredentials = new System.Net.CredentialCache();
NetworkCredential netCred = new NetworkCredential("User Id", "Password");
userCredentials.Add(new Uri(myService.Url), "Basic", netCred);
myService.Credentials = userCredentials;
You can also use Default or Network Credentials like below
C#.NET code Sample for Default Credential
// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
myService.Credentials = System.Net.CredentialCache.DefaultCredentials;
C#.NET code Sample for Default Network Credential
// MyWebService is the web service reference to your project
MyWebService myService = new MyWebService();
myService.Timeout = -1;
myService.PreAuthenticate = true;
myService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Now you can call methods on the myService to use.
No comments:
Post a Comment