C# 为WCF应用程序设置凭据?

C# 为WCF应用程序设置凭据?,c#,.net,wcf,C#,.net,Wcf,我编写了一个简单的应用程序,它利用WCF在客户端和服务器之间进行通信。当我在本地运行它时,它可以正常工作,但是当我在两个不同的框上运行服务器和客户端时,会出现以下异常: Unexpected error occured, while getting the group names from the VDN server System.ServiceModel.Security.SecurityNegotiationException: The server has rejected the cl

我编写了一个简单的应用程序,它利用WCF在客户端和服务器之间进行通信。当我在本地运行它时,它可以正常工作,但是当我在两个不同的框上运行服务器和客户端时,会出现以下异常:

Unexpected error occured, while getting the group names from the VDN server
System.ServiceModel.Security.SecurityNegotiationException: The server has rejected the client credentials.
System.Security.Authentication.InvalidCredentialException: The server has rejected the client credentials.
System.ComponentModel.Win32Exception: The logon attempt failed
哪些凭据未被接受?我如何设置它们

有没有办法将服务器配置为不需要身份验证?该应用程序是一个简单的监控应用程序,安全性并不是一个真正的问题

很抱歉说得不太具体: 该应用程序使用管道代理,没有wcf配置文件,因为wcf代码是手工编码的

我的WCF代码基于本教程中的代码:

直到我写完所有代码之后,我才知道从配置生成wcf类是一个标准过程。现在,每当我查看教程/帮助文档时,它们都会使用生成的代码,所有内容都需要更改配置


我没有足够的带宽(我已经处理了3个项目)用一个tht使用生成的代码替换我的wcf组件,但我会确保下次使用wcf时使用生成的代码。

我在搜索禁用wcf安全性/身份验证时找到了一个解决方案

发件人:

或在配置中添加以下内容:

<wsHttpBinding>

    <binding name="myBinding">

        <security mode="None" />

    </binding>

</wsHttpBinding>

创建这样的方法

void SetImpersonation(ref IServiceClient proxy)
{
  proxy.ClientCredentials.Windows.ClientCredential.Domain = "MYDOMAIN";
  proxy.ClientCredentials.Windows.ClientCredential.UserName = "A_USER";
  proxy.ClientCredentials.Windows.ClientCredential.Password = "P4SSW0RD";
}
并在创建新客户机类时调用它

IServiceClient proxy = new IServiceClient ();
SetImpersonation(ref proxy);

显然,这是将信息设置为特定用户,如果代码被反编译,则会产生安全影响,但如果您使用具有以下安全性的WCF绑定配置,则它应该在(无配置文件的场景)中工作。

<transport clientCredentialType="Windows" />

我认为你需要更具体一些。您使用的绑定是什么?我可以将这些值迁移到app.config中。我只是没有wcf代码的配置文件。谢谢,虽然看起来不错。如何避免在每次使用我的客户时设置此选项?有没有办法为我的解决方案中的所有客户端设置此选项?使用此解决方案时必须小心,因为原始异常可能来自使用Windows身份验证的绑定配置。在这种情况下,盲目地移除安全是一个坏主意。如果使用Windows身份验证,请检查底部的“我的答案”。
<transport clientCredentialType="Windows" />
'Create an instance of the WCF service      
Dim MyService As New MyWCFServiceClient

'Build credentials object for which this WCF call will be made
MyService.ClientCredentials.Windows.ClientCredential = New System.Net.NetworkCredential("UserName", "Password", "DomainName")

'Call a method on the service
MyService.MyMethod1()