如何使用用户名和密码进行身份验证托管WCF服务?

如何使用用户名和密码进行身份验证托管WCF服务?

问题描述:

我已经托管在正常运行中的服务器的服务器的WCF服务。我尝试添加这也是在另一台服务器成功加入该服务的服务引用,然后尝试以下

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

但我得到异常的错误(请求已中止:请求已被取消)。而传输通过HTTP通道数据发生了

配置文件

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

首先,在设置WCF服务/客户端这是非常有用的配置WCF跟踪,以获得更详细的和用户友好的错误消息。有关WCF的详细信息跟踪,请参阅 WCF跟踪

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.

假设你只想要一个简单的用户名和密码授权,而不是Windows一个比HTTPS。那么你的安全部分应该是这样的:

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>

然后你可以使用这个code到指定的客户端凭据。

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