Jameel Moideen
Jameel Moideen

Reputation: 7931

How to authenticate hosted wcf service with username and password?

I have hosted a wcf service in an server which runs fine in that server. I try to add a service reference of that service which also added successfully in another server and then try to authenticate these service like below

var engServiceClient = new EngServiceClient();
engServiceClient.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Anonymous;
engServiceClient.ClientCredentials.Windows.ClientCredential.Domain = @"test";
engServiceClient.ClientCredentials.Windows.ClientCredential.UserName = @"hello";
engServiceClient.ClientCredentials.Windows.ClientCredential.Password = @"123wcf";
var getData = engServiceClient.GetActiveEngagements();

but I got exception An error (The request was aborted: The request was canceled.) occurred while transmitting data over the HTTP channel.

Configuration File generated after adding the service reference

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="EngService" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://testwebdemo-t.orioninc.com:8443/Services/2012v2/EngService.svc"
                binding="basicHttpBinding" bindingConfiguration="EngService"
                contract="ServiceReference1.IEngService" name="EngService" />
        </client>
    </system.serviceModel>
</configuration>

How can i authenticate this service by using my username and password?

Upvotes: 1

Views: 1151

Answers (1)

Maksym Strukov
Maksym Strukov

Reputation: 2689

First of all, while setting up WCF services/clients it's very useful to configure WCF tracing to get more detailed and user friendly error messages. For more info about WCF tracing please refer to WCF Tracing

What kind of client authorization are you trying to implement? I can see you specified Windows type for transport security and UserName for message security which is both redundant and confusing.

Suppose you just want a simple username and password authorization not Windows one over https. Then your security section should look like this:

<security mode="TransportWithMessageCredential">
        <transport clientCredentialType="None" />
        <message clientCredentialType="UserName" establishSecurityContext="true"/>
      </security>

Then you can use this code to specify your credentials on the client side.

client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";

Hope it helps

Upvotes: 3

Related Questions