如何将凭据从一个Web服务传递到另一个Web服务?

时间:2012-01-25 19:34:45

标签: c# web-services .net-3.0 networkcredentials defaultnetworkcredentials

我目前有一个调用Web服务(WS1)的应用程序,后者又调用另一个Web服务(WS2)来获取/设置WS2上托管的服务器上的信息。我希望能够将用户凭据从WS1传递到WS2,就像有一个应用程序直接调用WS2一样。有没有办法做到这一点?

这就是我目前的情况:

申请代码:

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代码(在'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;

它是Web服务代码中我需要更改的最后一行,我知道......但我不知道该将它设置为... 有ClientCredentials.UserName但我没有此级别的密码。

2 个答案:

答案 0 :(得分:0)

这通常通过CAS(http://www.jasig.org/cas)等集中认证服务完成。

答案 1 :(得分:-3)

我不使用C#进行编码,但看起来您想要的是使用您的网络服务电话发布凭据。

为此,您需要将凭据附加到HTTP请求的正文中。