简体   繁体   中英

Setting credentials for a WCF application?

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:

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.

My WCF code is based on the code in this tutorial: 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.

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.

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

From 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>

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)

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

<transport clientCredentialType="Windows" />

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

'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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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