使用wshttpbinding无效的WCF用户名和密码验证

问题描述 投票:0回答:2

我是WCF服务身份验证的新手,我试图使用wshttpbinding实现wcfauthentication。但我正在低于例外。

找不到与绑定WSHttpBinding的端点的方案https匹配的基址。注册的基地址方案是[http]。

Web.Config中:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>  
    <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttp">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WCFAuth.Service1" behaviorConfiguration="wsHttpBehavior">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="WCFAuth.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:64765/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="wsHttpBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFAuth.ServiceAuthanticator, WCFAuth"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

服务认证类:

using System;
using System.Collections.Generic;
using System.IdentityModel.Selectors;
using System.Linq;
using System.ServiceModel;
using System.Web;

namespace WCFAuth
{
    public class ServiceAuthanticator : UserNamePasswordValidator
    {

        public override void Validate(string userName, string password)
        {
            string AppUserName = "ABC";
            string AppPwd = "abc";
            try
            {
                if (userName.ToLower() != AppUserName.ToLower() && password != AppPwd)
                {                        
                    throw new FaultException("Unknown Username or Incorrect Password");
                }
            }
            catch (Exception ex)
            {                    
                throw new FaultException("Unknown Username or Incorrect Password");
            }
        }
    }
}

客户端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <!--<binding name="base" />-->
              <binding name="base">
                <security mode="TransportCredentialOnly">
                  <transport clientCredentialType="Basic"/>
                </security>
              </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:64765/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="base" contract="WCFAuth.IService1" name="base" />
        </client>
    </system.serviceModel>
</configuration>

消费者:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                WCFAuth.Service1Client client = new WCFAuth.Service1Client();                
                client.ClientCredentials.UserName.UserName = "test";
                client.ClientCredentials.UserName.Password = "test";                
                var temp = client.GetData(1);
                Console.WriteLine(temp);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
    }

当我尝试浏览svc文件时,我得到附加异常。

enter image description here

有人可以纠正我,我犯了错误,提前谢谢。

c# .net wcf iis wcf-authentication
2个回答
1
投票

这里的问题是您正在使用带传输安全性的WSHttpBinding,但您设置的基址是http。这里不能使用http,因为您通过网络发送凭据。

将其更改为https,或者为开发目的创建第二个绑定配置。一个使用Transport Security(https),另一个没有(http)。

还要确保客户端绑定与服务器的绑定匹配。


0
投票

正如Marc所说,我们应该在托管服务时提供证书。在托管服务的过程中可能会有一些不妥之处。 这是一个参考配置,希望它对您有用。

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttp">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="WCFAuth.Service1" behaviorConfiguration="wsHttpBehavior">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="WCFAuth.IService1">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="wsHttpBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFAuth.ServiceAuthanticator, WCFAuth"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

然后我们应该在IIS Site Bindings模块中添加一个https绑定。 enter image description here 服务地址是https://x.x.x.x:8865/Service1.svc 必须注意的是,当我们通过添加服务引用来调用服务时,我们应该信任服务证书。

ServicePointManager.ServerCertificateValidationCallback += delegate
              {
                  return true;
              };
            ServiceReference2.Service1Client client = new ServiceReference2.Service1Client();
            client.ClientCredentials.UserName.UserName = "jack";
            client.ClientCredentials.UserName.Password = "123456";

此外,如果我们使用SecurityMode.Message,我们应该在代码片段中提供证书。

  <serviceCredentials>
            <serviceCertificate storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" findValue="869f82bd848519ff8f35cbb6b667b34274c8dcfe"/>
            <userNameAuthentication customUserNamePasswordValidatorType="WcfService1.CustUserNamePasswordVal,WcfService1" userNamePasswordValidationMode="Custom"/>
          </serviceCredentials>

如果有什么我可以帮忙,请随时告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.