WCF中的安全协商异常

时间:2011-08-18 09:00:21

标签: wcf wcf-security

考虑:

Enter image description here

我在连接到WCF服务时遇到此错误。我在WCF服务中有WCF用户名和密码身份验证。我希望客户端需要用户名和密码才能连接到此服务。这是我的服务配置:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="NewBinding0">
                <security>
                    <message clientCredentialType="UserName" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="WcfUserNamePasswordAuthentication.Service1Behavior"
                 name="WcfUserNamePasswordAuthentication.Service1">
            <endpoint address="" binding="wsHttpBinding" contract="WcfUserNamePasswordAuthentication.IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WcfUserNamePasswordAuthentication.Service1Behavior">

                <!-- To avoid disclosing metadata information, set the value below to
                     false and remove the metadata endpoint above before deployment. -->
                <serviceMetadata httpGetEnabled="true"/>

                <!-- To receive exception details in faults for debugging
                     purposes, set the value below to true. Set to false
                     before deployment to avoid disclosing exception
                     information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>

                <serviceCredentials>
                    <serviceCertificate findValue="CertMohan"
                                        storeLocation="LocalMachine"
                                        storeName="My"
                                        x509FindType="FindBySubjectName" />
                    <userNameAuthentication userNamePasswordValidationMode="Custom"
                                            customUserNamePasswordValidatorType="WcfUserNamePasswordAuthentication.UserNameAuthentication, WcfUserNamePasswordAuthentication"
                    />
                </serviceCredentials>

            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

这是我的客户端配置:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
               openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
               bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
               maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
               textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">

                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                 maxBytesPerRead="4096" maxNameTableCharCount="16384" />

                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                 enabled="false" />

                <security mode="Message">

                    <message clientCredentialType="UserName" negotiateServiceCredential="true"
                     algorithmSuite="Default" establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://abc31.org.in/WcfUserName/Service1.svc"
           binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
           contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">

            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

这样我连接到我的服务:

ServiceReference1.Service1Client sc = new WebApplication3.ServiceReference1.Service1Client();
EndpointIdentity.CreateDnsIdentity("localhost");
var binding = new WSHttpBinding();

binding.Security.Mode = SecurityMode.Message;

binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
sc.ClientCredentials.UserName.UserName = "Mohan";
sc.ClientCredentials.UserName.Password = "Sharma";
lblRecord.Text = sc.GetData(1000);

1 个答案:

答案 0 :(得分:1)

DnsEndpointIdentity不能是localhost。它必须是您所在案例CertMohan中的证书的主题名称。

相关问题