How to pass credentials from one web service to another?

2k views Asked by At

I currently have an app that calls a web service(WS1), which in turn calls another web service(WS2) to get/set information on the server hosted on WS2. I would like to be able to pass in the user credentials into WS2 from WS1 as if there was an application calling directly into WS2. Is there a way to do this?

This is what I have currently:

Application Code:

BasicHttpBinding basicHttpBinding = 
    new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);

basicHttpBinding.Security.Transport.ClientCredentialType = 
    HttpClientCredentialType.Windows;

basicHttpBinding.MaxReceivedMessageSize = 131072000;

AppMgr.AppMgrSoapClient appMgr = 
    new AppMgr.AppMgrSoapClient(
        basicHttpBinding, 
        new EndpointAddress(@"http://SomeServer/Service.asmx"));

appMgr.ClientCredentials.Windows.AllowedImpersonationLevel =
    TokenImpersonationLevel.Impersonation;

appMgr.ChannelFactory.Credentials.Windows.ClientCredential = 
    CredentialCache.DefaultNetworkCredentials;

appMgr.SomeWebMethodCall();

Web Service 1 code (on 'SomeServer')

BasicHttpBinding basicHttpBinding = 
    new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);

basicHttpBinding.Security.Transport.ClientCredentialType = 
    HttpClientCredentialType.Windows;

basicHttpBinding.MaxReceivedMessageSize = 131072000;

WS2Service.WS2ServiceSoapClient myServiceReference = 
    new WS2Service.WS2ServiceSoapClient(
        basicHttpBinding,
        new EndpointAddress(@"http://SomeOtherServer/AnotherService.asmx"));

myServiceReference.ClientCredentials.Windows.AllowedImpersonationLevel = 
    TokenImpersonationLevel.Impersonation;

myServiceReference.ChannelFactory.Credentials.Windows.ClientCredential = 
    CredentialCache.DefaultNetworkCredentials;

Its the last line in the Web Service code that I need to change, I know that ... but I don't know what to set it to ... There is ClientCredentials.UserName but I don't have the password at this level.

2

There are 2 answers

1
Quasar On

I do not code in C# , but looks like what you'd want is to post the credentials using your web service call.

For that you need to append the credentials to the body of the HTTP request.

0
hockey_dave On

This is typically done via centralized authentication service like CAS (http://www.jasig.org/cas).