Search code examples
c#wcf.net-6.0wcf-security

Generated WCF SOAP client uses current user for windows authentication instead of given credentials


I'm kind of new to the whole WCF and SOAP topic so please be kind.

I'm using a generated SOAP Client with .net6. In another project we successfully worked with the same Web Service using the old .net Framework 2.0 Web References and the same credentials.

Strange enough everything seemed to work fine at first. Until I realized, that it does not use the given credentials to authenticate. Instead it authenticates with my own domain user. I also tried to get it to work with explicitly setting the binding with a BasicHttpBinding but I only could get the same broken logic to work or I got various authentication/protocol/security errors.

So it seems the authentication is basically working. It just doesn't use the provided credentials. So my question is: How can I configure it to work with the provided identity?

I also found out that it might have anything to do with a cached Windows token. But how can I get rid of it. How to prevent caching in the first place?

EDIT: Specified the variable types explicitly.

string url = "http://someServer/AdministrationService.asmx";
AdministrationServiceSoapClient client = new AdministrationServiceSoapClient(
    AdministrationServiceSoapClient.EndpointConfiguration.AdministrationServiceSoap,
    url);

WindowsClientCredential credential = client.ClientCredentials.Windows;

credential.ClientCredential.UserName = "username";
credential.ClientCredential.Password = "password";
credential.ClientCredential.Domain = "DOMAIN";

GetServerInfoRequest getServerInfoRequest = new GetServerInfoRequest
                               {
                                  // some stuff set here
                               };

GetServerInfoRequest getServerInfoReply = await client.GetServerInfoAsync(getServerInfoRequest);

Solution

  • After waiting a day it is working. It seems that the cached credentials became invalid somehow.

    Strange enough the simple service creation from above is not working anymore. Instead I have to use the following.

    var client = new AdministrationServiceSoapClient(
        new BasicHttpBinding()
        {
            Security = new BasicHttpSecurity()
                       {
                           Mode = BasicHttpSecurityMode.TransportCredentialOnly,
                           Message = new BasicHttpMessageSecurity()
                                     {
                                         ClientCredentialType = BasicHttpMessageCredentialType.UserName,
                                     },
                           Transport = new HttpTransportSecurity()
                                       {
                                           ClientCredentialType = HttpClientCredentialType.Windows,
                                           ProxyCredentialType = HttpProxyCredentialType.Windows,
                                       }
                       },
        },
        new EndpointAddress(url));