繁体   English   中英

基本身份验证和WCF

[英]Basic authentication and WCF

我正在尝试学习WCF,但是我真的不明白我该怎么做。 我有一个包含用户名和密码的数据库,用户在使用该服务之前应先进行身份验证。

现在,用户名和密码已进行硬编码:

class UsernameAuthentication : UserNamePasswordValidator
{
    /// <summary>
    /// When overridden in a derived class, validates the specified username and password.
    /// </summary>
    /// <param name="userName">The username to validate.</param><param name="password">The password to validate.</param>
    public override void Validate(string userName, string password)
    {
        var ok = (userName == "test") && (password == "test");
        if (ok == false)
            throw new AuthenticationException("username and password does not match");
    }
}

我的服务很简单:

public class Service1 : IService1
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Subtract(int a, int b)
    {
        return a - b;
    }
}

我的问题是:为了使这项工作有效,我到底需要在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="Binding1">
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.UsernameAuthentication, service1" />
          </serviceCredentials>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 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无法激活。

您必须在web.config中指定将使用用户名/密码凭据,并使用自定义密码验证器。

服务的绑定应设置一种安全性类型(“ Transport或“ Message ,最适合您),对于这种安全性,必须设置要使用的凭据(用户名和密码)。

<system.serviceModel> 
  <bindings>
  <wsHttpBinding>
      <binding name="Binding1" ...>
        <security mode="Message">
          <message clientCredentialType="UserName" />
        </security>
      </binding>        
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

其中...表示您的服务特有的许多其他设置。

考虑到只有某些类型的绑定和安全模式支持这种类型的凭据,但是MSDN拥有您可能需要的所有信息。

如果未将凭据设置为用户名和密码,则不会以这种方式对用户进行身份验证。

要告诉服务使用您的密码验证器,您需要添加以下内容:

<behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
            <serviceCredentials>
              <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" />
            </serviceCredentials>
         .....
         </serviceBehaviors>
</behaviors> 

其中Microsoft.ServiceModel.Samples.CalculatorService是您具有自定义验证器的名称空间, CustomUserNameValidator是自定义验证器(在您的情况下为UserNamePasswordValidator ),而serviceservice的名称。

否则,该服务将需要一个默认验证器,例如ASP.NET Membership Provider。

服务凭证必须放在您的服务行为中。

另外,不要忘记将行为链接到服务定义。

<services>
  <service behaviorConfiguration="ServiceBehavior" name="ServiceName">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="ContractName" />
     ....
  </service>
</services>

注意 :web.config中还有许多未显示的设置。 元素的名称仅是定向的。 这只是为了使用户名凭据起作用。

您可以查看MSDN,因为他们对此有很多伟大的教程,像这样一个http://msdn.microsoft.com/en-us/library/aa702565.aspxhttp://msdn.microsoft.com/en-us/ library / aa354513.aspx

是的,实际上,如果您以正确的方式进行配置,它将在授予客户端(用户,客户端服务)运行服务方法权限之前对其进行身份验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM