如何使用用户名和密码验证托管的wcf服务?

时间:2013-01-14 09:48:42

标签: .net wcf

我在服务器中托管了一个wcf服务,该服务器在该服务器上运行良好。我尝试添加该服务的服务引用,该服务也在另一台服务器中成功添加,然后尝试对这些服务进行身份验证,如下所示

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>

如何使用我的用户名和密码验证此服务?

1 个答案:

答案 0 :(得分:3)

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

您尝试实施哪种客户端授权?我可以看到您为传输安全性指定了Windows类型,为邮件安全指定了UserName,这既冗余又令人困惑。

假设您只需要一个简单的用户名和密码授权,而不是Windows上的https。然后您的安全部分应如下所示:

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

然后,您可以使用此代码在客户端指定您的凭据。

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

希望有所帮助