Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有自定义用户名和密码身份验证的wcf服务_C#_Wcf_.net 4.0_.net 2.0_Wcf Binding - Fatal编程技术网

C# 具有自定义用户名和密码身份验证的wcf服务

C# 具有自定义用户名和密码身份验证的wcf服务,c#,wcf,.net-4.0,.net-2.0,wcf-binding,C#,Wcf,.net 4.0,.net 2.0,Wcf Binding,我正在尝试使用自定义用户名和密码验证器访问wcf服务。。 在本例中,我有一个运行在.net 2.0中的win console应用程序和运行在.net 4.0中的wcf服务 向服务添加web引用后,我可以调用该方法并在控制台中打印结果 现在我想增加一些安全性。。 我对web/wcf服务的身份验证过程有点陌生。。 在我创建的第一个项目中,我使用了soap头,但在本例中,这两个头都运行在.NET4.0中 我确实发现有一些方法可以做到这一点,但作为初学者,我想了解基本方法,在本例中,发送用户名和密码似乎

我正在尝试使用自定义用户名和密码验证器访问wcf服务。。 在本例中,我有一个运行在.net 2.0中的win console应用程序和运行在.net 4.0中的wcf服务 向服务添加web引用后,我可以调用该方法并在控制台中打印结果

现在我想增加一些安全性。。 我对web/wcf服务的身份验证过程有点陌生。。 在我创建的第一个项目中,我使用了soap头,但在本例中,这两个头都运行在.NET4.0中

我确实发现有一些方法可以做到这一点,但作为初学者,我想了解基本方法,在本例中,发送用户名和密码似乎是一种简单的方法。。 因此,为了做到这一点,我需要创建一个新类来扩展UserNamePasswordValidator并重写validate方法

 public override void Validate(string userName, string password)
        {
            if (userName != "teste")
                throw new SecurityTokenException("Unknown Username or Password");
         }
我遇到的问题是将WebService的web配置文件配置为使用此自定义验证器

这是我的web.config

    <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="secureHTPPS">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>

        <behavior>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="Services.CustomUserNamePasswordValidator, Services" />
          </serviceCredentials>
          <!-- To avoid disclosing metadata information, set the value below to false 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"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </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>
但在本例中,它在clientCredentials上给了我一个错误,它缺少一个引用。。 但此参考仅在.NET3.0及更高版本上可用

想法是发送用户名和密码,然后验证用户名,然后调用方法

我该怎么做

我一直在解决这个问题,但我没有设法让它工作

所以我需要使用basichttpbinding,因为控制台应用程序和服务上的框架使用不同

我需要将iis配置为使用https…为此,我遵循了以下示例:

我按照本页创建服务示例。。

这现在是我的web.config文件

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1"
              behaviorConfiguration="Brett_Behavior">
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="Brett_BindingConfiguration"
                  contract="WcfService1.IService1" />
        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="Brett_BindingConfiguration">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Brett_Behavior">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="WcfService1.Auth, WcfService1"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </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>

当我选择service1.svc并尝试在浏览器中查看时,现在会出现以下错误:

找不到绑定为BasicHttpBinding的终结点的匹配方案https基址。注册的基址方案为[http]


我需要做什么?

为什么您要尝试使用2.0客户端和4.0服务?如果我使用基本http绑定,可能会出现重复。我忘了提到wcf服务和控制台应用程序之间的通信正在工作,我正在尝试了解我需要做些什么才能使身份验证工作正常。。。
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1"
              behaviorConfiguration="Brett_Behavior">
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="Brett_BindingConfiguration"
                  contract="WcfService1.IService1" />
        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="Brett_BindingConfiguration">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Brett_Behavior">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="WcfService1.Auth, WcfService1"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </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>