简体   繁体   English

如何正确地将 System.Net.NetworkCredential 传递给 WCF 客户端?

[英]How do I properly pass System.Net.NetworkCredential to WCF Client?

I currently have client device that prompts the user for user/pass and creates a System.Net.NetworkCredential that is passed to a WCF Service.我目前有客户端设备,提示用户输入用户/密码并创建一个传递给WCF服务的System.Net.NetworkCredential This WCF Service then uses the credential to use on another external service.然后,此WCF服务使用凭据在另一个外部服务上使用。

The problem is that whenever I pass through the System.Net.NetworkCredential to the service it seems to lose the password field and so I never get it to authenticate with the external service.问题是,每当我将System.Net.NetworkCredential传递给服务时,它似乎都会丢失密码字段,因此我从未让它与外部服务进行身份验证。 I've tested just passing in plain text/strings as user/pass and it works but feel like this is not great for security (although I do have HTTPS enabled for all traffic between the two services).我已经测试过只将纯文本/字符串作为 user/pass 传递,它可以工作,但感觉这对安全性不是很好(尽管我确实为两个服务之间的所有流量启用了 HTTPS)。

Is there a way to pass these credentials to the service correctly?有没有办法将这些凭据正确传递给服务?

Here is some test code on the WCF service:以下是WCF服务的一些测试代码:

public string TestCred(System.Net.NetworkCredential c, string username, string pass)
{

    cred = c;
    userName = username;
    passWord = pass;
    string s = cred.UserName + cred.Password + "end";
    return s;

}

I have it return that string to test what is actually received at the Service side and the password is completely blank (the username makes it there ok).我让它返回该字符串以测试在服务端实际收到的内容并且密码完全为空(用户名使其正常)。

Here is the code I use on the WCF service to contact the external service:这是我在WCF服务上用于联系外部服务的代码:

externalService.Credentials =  new System.Net.NetworkCredential(cred.UserName, cred.Password, "domain");

This doesn't work since the cred.Password field is blank, also I've tried:这不起作用,因为 cred.Password 字段为空,我也试过:

externalService.Credentials = cred;

Which also fails.这也失败了。

When I use the plain strings though it works great:当我使用普通字符串时,虽然效果很好:

externalService.Credentials =  new System.Net.NetworkCredential(userName, passWord, "domain");

Any suggestions?有什么建议? Since the connection between the client device-WCF and Service-External Service are all HTTPS-SSL required, it is slightly secure but I didn't feel great passing user/pass with strings.由于客户端设备-WCF 和服务-外部服务之间的连接都需要HTTPS-SSL ,所以它有点安全,但我觉得用字符串传递用户/传递不是很好。

Have you looked at the TokenImpersonationLevel?你看过 TokenImpersonationLevel 吗? MSDN: TokenImpersonationLevel MSDN:TokenImpersonationLevel

                var credentials = new ClientCredentials();
            credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

            reportChannel.Endpoint.Behaviors.Remove<ClientCredentials>();
            reportChannel.Endpoint.Behaviors.Add(credentials);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM