使用TransportWithMessageCredential时,CredentialCache.DefaultNetworkCredentials为空

时间:2012-05-24 20:07:27

标签: wcf wcf-binding wcf-security

我目前正在尝试在我的wcf项目中使用TransportWithMessageCredentials和https。我已经设置了客户端和服务器安全模式=“TransportWithMessageCredential”和clientCredentialType =“Windows”。

当我从CredentialCache.DefaultNetworkCredentials获取凭据时,用户名,密码和域都是空的。

为什么这些会是空的?如果它们总是空的,我将从哪里获得凭证?

如何在不提示登录的情况下将登录的用户凭据传递给服务?

客户端绑定

<basicHttpBinding>
    <binding name="ClientHttpEndpoint" closeTimeout="00:01:00"
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:02:00"
    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
    maxBufferSize="4194304" maxBufferPoolSize="524288" maxReceivedMessageSize="4194304"
    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
    useDefaultWebProxy="true">
        <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
    </binding>
</basicHttpBinding>

服务器绑定

<basicHttpBinding>
    <binding name="WindowsTransportCredentialBinding" maxBufferSize="524288"maxReceivedMessageSize="524288">
        <readerQuotas maxDepth="128" maxStringContentLength="1048576" />
        <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Windows" />
        </security>
    </binding>
</basicHttpBinding>

...

<service name="Test.DiagnosticService">
    <endpoint binding="basicHttpBinding" bindingConfiguration="WindowsTransportCredentialBinding" name="ClientDiagnosticEndpoint" contract="Test.IDiagnostic" />
</service>

设置用户名和密码的代码

ChannelFactory<IDiagnostic> test = new ChannelFactory<IDiagnostic>(DIAGNOSTIC_ENDPOINT_NAME);
test.Credentials.UserName.UserName = "TestUser";
test.Credentials.UserName.Password = "User";
return test.CreateChannel();

1 个答案:

答案 0 :(得分:0)

DefaultCredentials返回的ICredentials实例不能用于查看当前安全上下文的用户名,密码或域。

http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultcredentials%28v=vs.90%29.aspx