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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/864202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:44:04  来源:igfitidea点击:

Setting credentials for a WCF application?

c#.netwcf

提问by Omar Kooheji

I've written a simple Application that utilizes WCF to communicate between client and server. When I run it locally it works fine, however when I run the server and client on two different boxes I get the following exception:

我编写了一个简单的应用程序,它利用 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

What are the credentials that are not being accepted? And how can I set them?

哪些凭证不被接受?我该如何设置它们?

Is there a way to configure the server to not require authentication? The application is a simple monitoring app to security is not really an issue.

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

Sorry about not being very specific:The app uses a pipe proxy and there is no wcf config file as the wcf code is hand coded.

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

My WCF code is based on the code in this tutorial: http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

我的 WCF 代码基于本教程中的代码:http: //www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

I didn't konw it was standard proctice to generate the wcf classes from a config till after I'd finished writing all the code. Now whenever I look at a tutorial/ help doc they use generated code and everything requires changing the config.

我不知道从配置生成 wcf 类是标准过程,直到我完成所有代码的编写。现在,每当我查看教程/帮助文档时,他们都会使用生成的代码,并且一切都需要更改配置。

I don't have the bandwidth (I'm juggling 3 projects already) to replace my wcf component with one tht uses generated code but I will make sure to use the code generation next time I use wcf.

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

采纳答案by ZombieSheep

Create a method like this...

创建一个这样的方法......

void SetImpersonation(ref IServiceClient proxy)
{
  proxy.ClientCredentials.Windows.ClientCredential.Domain = "MYDOMAIN";
  proxy.ClientCredentials.Windows.ClientCredential.UserName = "A_USER";
  proxy.ClientCredentials.Windows.ClientCredential.Password = "P4SSW0RD";
}

and call it when you create the new client class.

并在您创建新的客户端类时调用它。

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

Obvously, this is setting the information to be a specific user, and has security implications if your code is decompiled, but it should work in your (config file-less scenario)

显然,这是将信息设置为特定用户,如果您的代码被反编译,则具有安全隐患,但它应该适用于您的(无配置文件场景)

回答by mirezus

Here's a solution I found in a search for disabling wcf security/authentication.

这是我在搜索禁用 wcf 安全/身份验证时找到的解决方案。

From MSDN:

MSDN

WSHttpBinding b = new WSHttpBinding();

b.Security.Mode = SecurityMode.None;

or add the following in config:

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

<wsHttpBinding>

    <binding name="myBinding">

        <security mode="None" />

    </binding>

</wsHttpBinding>

回答by atconway

If you are using a WCF binding configuration with the following security:

如果您使用具有以下安全性的 WCF 绑定配置:

<transport clientCredentialType="Windows" />

then you will need to explicitly set the Windows Credentials on the WCF instance created in code like below:

那么您需要在如下代码中创建的 WCF 实例上显式设置 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()