【问题标题】:Security negotiation exception in WCFWCF 中的安全协商异常
【发布时间】:2011-10-29 14:37:41
【问题描述】:

考虑:

我在连接到 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);

【问题讨论】:

    标签: wcf wcf-security


    【解决方案1】:

    DnsEndpointIdentity 不能是 localhost。它必须是证书的主题名称,在您的情况下为 CertMohan。

    【讨论】:

    • 感谢您的回复。它没有用。仍然会出现同样的错误。
    • 你在服务器和客户端都改了吗?证书是否受客户端信任?
    • 这是selfcert创建的测试证书。我在两边都改了。
    • 虽然我的问题是这样,但我将其标记为答案,因为它也可能会产生错误。有什么你能想到的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2011-02-21
    相关资源
    最近更新 更多