WCF中的安全协商异常

发布于 2024-11-30 05:24:25 字数 4778 浏览 0 评论 0原文

请考虑:

在此处输入图像描述

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

Consider:

Enter image description here

I'm getting this error while connection to a WCF service. I have WCF username and password authentication in the WCF service. I want clients to require a username and password to connect to this service. This is my service configuration:

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

And this is my client configuration:

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

And this way I am connecting to my service:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2024-12-07 05:24:25

DnsEndpointIdentity 不能是 localhost。它必须是您的 CertMohan 中的证书的使用者名称。

DnsEndpointIdentity cannot be localhost. It must be subject name of the certificate which is in your case CertMohan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文