SoapUI basic authentication to c# code - web-services

I have problem with web service I want to connect to.
In soapUI i used basic authentication with username and password (domain empty), and changed WSS-Password Type to PasswordText and I could without a problem connect to web service. But when I try to connect using c# code:
WebService service = new WebService();
service.ClientCredentials.UserName.UserName = "user";
service.ClientCredentials.UserName.Password = "pass";
service.SomeMethod();
It returns "Forbidden" error.

Related

MSCRM 2013 - The caller was not authenticated by the service

I am trying to call CRM webservice from my custom WCF service. And I am getting the exception "The caller was not authenticated by the service". Checked the authentication mode in IIS (where my wcf service is hosted). Its set to anonymous authentication enabled.
Is there any reason why MSCRM service would fail authentication while call is made from WCF?
Here is my code which calls the CRM web service
OrganizationService service;
var crmConnection = CrmConnection.Parse("Server=http://myserver/orgname; Domain=domainname; Username=username; Password=passwordtext");
service = new OrganizationService(crmConnection);
Entity crmEvent = service.Retrieve("new_event", eventId, new ColumnSet("status"));
When using windows authentication you need to pass a Networkcredential to the constructor of the OrganizationService.
This was already answered by #Daryl here.

Retrieve username from UsernameToken scenario in web service on WSO2 Application Server

I have a WSO2 ESB WSDL based proxy service which is secured with UsernameToken scenario. This wrapper service is for an Axis2 back-end web service deployed as an .aar file in the WSO2 Application Server. What I want is to retrieve the username used for the authentication in the back-end service.
I tried to get the username using the below Java code, but it always comes back NULL:
MessageContext msgContext = MessageContext.getCurrentMessageContext();
String username = (String)msgContext.getProperty(RampartMessageData.USERNAME);
It seems that the MessageContext does not store the Rampart message data. Is there anything I can set in the WSO2 ESB to have that username be passed to the back-end service in the message context?
Thank you!
I guess, You have written a class mediator to retrieve the username from message context. Please note that, username is not in the synapse message context. It is in the axis2 message context. Please try with following code.. inside the your mediator. It has been worked for me.
Axis2MessageContext axis2Msgcontext = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext msgContext = axis2Msgcontext.getAxis2MessageContext();
String userName = (String) msgContext.getProperty("username");
Or else, do you want to retrieve username in BE service (inside AS)? Then, you need to send the user name to BE services. You can use HTTP header, or any other method to send the user name to the BE service. Then you can retrieve it by using Axis2 handler. As an example you can set the user name in to HTTP header as following using synapse configuration in the WSO2ESB. Then header value would be passed to BE service
<header name="UserName" value="Test" />

Salseforce Apex classes support Apache axis Stub authentication

We have converted a WSDL file of a Web serivice into the salesforce apex classes. The Web Service is receiving the authentication credentials in Apache axis Stub authentication username and password format.
Below is the sample Apache axis Stub authentication username and password code.
Service service = new XYZServiceLocator();
URL endpointURL = new URL("https://urllink");
XYZServiceSoapBindingStub stub = new XYZServiceSoapBindingStub(endpointURL, service);
stub.setUsername("username");// void org.apache.axis.client.Stub.setUsername(String username)
stub.setPassword("password");// void org.apache.axis.client.Stub.setPassword(String Password)
QueryResponse qresp = stub.webServiceCall(qr);
My question is. Can we get the Apache axis Stub authentication username and password functionality in the salesforce Apex classes.
As the Apex Stub support the HTTP Headers authentication does it also support the Apache axis Stub authentication?
Below is the Salesforce Apex stub HTTP Headers authentication code
String myData = 'username:password';
Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));
encodedusernameandpassword = EncodingUtil.base64Encode(hash);
XYZBillingStub.inputHttpHeaders_x.put('Authorization','Basic ' + encodedusernameandpassword );// SALESFORCE STUB
XYZBilling.query(queryReq )// Web Service call
Please help me in resolving this issue.
After converting the apex code to the below code I was successfully able to resolve the issue.
String myData = 'username:password';
encodedusernameandpassword = EncodingUtil.base64Encode(Blob.valueOf(myData));
XYZBillingStub.inputHttpHeaders_x.put('Authorization','Basic ' + encodedusernameandpassword );// SALESFORCE STUB
XYZBilling.query(queryReq )// Web Service call
This was a simple hit and trial solution I got, And I think Salseforce apex functionality only support input HTTP Headers authentication process. If one has some other way to do the authentication please mention it.
Looks like you already figured out a solution.
For reference, have a look at the Sending HTTP Headers on a Web Service Callout section of the online docs for doing basic authentication headers.

Custom Basic Authentication of a Web service

I'm having a small issue with implementing custom basic authentication for a asmx in .net 4.0.
I've created the HttpModule which would start the authentication process of the web service consumer runnig this code,
HttpApplication application = (source as HttpApplication);
HttpContext context = application.Context;
if ( VirtualPathUtility.GetFileName(context.Request.FilePath).Contains("svcEWS.asmx"))
{
string username = "", password = "";
string authHeader = HttpContext.Current.Request.Headers["Authorization"];
if (!string.IsNullOrEmpty(authHeader) && authHeader.StartsWith("Basic"))
{
//Authenticate here
}
}
However there is no authentication header present whenever this code is reached.
The consuming web app is simply calling,
EWS.svcEWS svcEWS = new EWS.svcEWS();
svcEWS.Credentials = new NetworkCredential("admin", "admin", "example.com");
svcEWS.HelloWorld();
IIS is set to run with anonymous authentication to anonymous authentication to prevent it from catching any auth requests.
Is there something I'm missing to have the client pass the correct header to my module?
I was forgetting to reject the first request with 401 code to force authentication. Doing so fixes the problem, as the invoker re sends the request with the auth header.

web service

I have web service, which use integrated security in IIS.
Now, I want to test my web service.
I created a console application and adding web reference.
now, when i m trying to use web method. It show 401 error.
m i missing any authentication.
I think i need to pass user name and password programatically.
can anyone direct me in right direction.
Set the web service credentials to the System.Net.CredentialCache.DefaultCredentials in your console app
MyWebService proxy = new MyWebService();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Edit
pass credentials programmatically:
MyWebService proxy = new MyWebService();
proxy.Credentials = new System.Net.NetworkCredential("username", "password", "domainname");
You need to add your windows id on your desktop to the allowed users in IIS.