Feroz M
Feroz M

Reputation: 11

SOAP Basic Authorization using C#

We had a web service consumed which was created in SAP. We are able to connect to the service successfully like using

var rowss = new EpiCompaniesImplServiceService();  //Service object creation
rowss.Credentials = new NetworkCredential("username", "password"); 

client suggested us to not to send credentials in plain text but in base64 encoded string using user name & password. For that, we have tried to add authorization header by converting credentails to base64 encoded way like below,

WebRequest myReq = WebRequest.Create(rowss.Url);
string usernamePassword = "username" + ":" + "password";
CredentialCache mycache = new CredentialCache();
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + usernamePassword);

but it was throwing 401 unauthorized error. Now client is asking us to try passing with SOAP header. Please suggest me some way to do it by passing encoded credentials through N/W credentials or by passing it through SOAP Headers.

Upvotes: 0

Views: 12157

Answers (1)

Timur Mannapov
Timur Mannapov

Reputation: 217

Seems like you are just missing encoding in base64:

 WebRequest myReq = WebRequest.Create(rowss.Url);
 string usernamePassword = "username" + ":" + "password";
 usernamePassword = Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)); // <--- here.
 CredentialCache mycache = new CredentialCache();
 myReq.Credentials = mycache;
 myReq.Headers.Add("Authorization", "Basic " + usernamePassword);

Upvotes: 1

Related Questions