对WCF休息服务的自定义基本身份验证不起作用

时间:2016-04-25 12:08:02

标签: c# wcf rest

我已按照其他主题的步骤进行操作:Adding basic HTTP auth to a WCF REST service

但是当我调用Web服务时,我没有被要求输入用户名和密码。我在UserNameAuthenticator类的所有方法中设置了断点,我发现任何这些方法都被调用。

所以我的问题是:这个类在哪里创建?在代码中我应该调用那些方法? web.config文件中是否有任何代码可以为我执行此操作?

提前致谢

1 个答案:

答案 0 :(得分:0)

为了设置WCF Web服务以在用户验证器中使用用户名和密码,您需要设置如下的服务行为:

<behaviors>
  <endpointBehaviors>
    <behavior name="webHttpBehavior">
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
      <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
      <serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentSessions="2147483647"
        maxConcurrentInstances="2147483647" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" 
                                customUserNamePasswordValidatorType="FullyQaulifiedNameSpace.UserValidatorClass, FullyQaulifiedNameSpace" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

用户名身份验证器(UserValidatorClass)将驻留在Web项目中,但可以从另一个引用的程序集中调用代码。 UserValidator代码如下所示:

using System.IdentityModel.Selectors;


namespace MyNameSpace.Web
{
public class UserValidator : UserNamePasswordValidator
{


    public UserValidator() : base()
    {

    }

    public override void Validate(string username, string password)
    {

    }
}
}

进入Validate方法的最简单方法是将您的Web服务发布到IIS,并附加到分配给您的Web服务的应用程序池的工作进程。