0

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.

3
  • I think you need to add this line mycache.Add(new Uri(rowss.Url), "Basic", basic);
    – Alex
    Commented Sep 11, 2015 at 10:32
  • it was not working like that. Tried that too before it self
    – Feroz M
    Commented Sep 11, 2015 at 10:45
  • Have you tried setting pre-authentication to true: rowss.PreAuthenticate = true;
    – Alex
    Commented Sep 11, 2015 at 13:06

1 Answer 1

1

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);
2
  • Timur, thanks for the response but we have tried the above code which i have mentioned with my question with base 64 encoded as you said and in normal way too but, both are not working. I guess there is a need to pass to SOAP Header or through network credentials. Any guessed how to do that?
    – Feroz M
    Commented Sep 11, 2015 at 11:12
  • Am I right that it worked without base64 and then when you encoded in base64 it stopped wotking? Commented Sep 14, 2015 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.