当前位置: 代码迷 >> Web Service >> customUserNamePasswordValidatorType 应该怎么设定
  详细解决方案

customUserNamePasswordValidatorType 应该怎么设定

热度:190   发布时间:2012-12-17 09:31:40.0
customUserNamePasswordValidatorType 应该如何设定
本帖最后由 nethawkc 于 2012-01-01 22:33:12 编辑 最近在学习 WCF 用户密码验证, 但是在设定customUserNamePasswordValidatorType 时却一直有问题.
我新增一个WCF的新网站, 不做任何修改╋只是在Service.cs 加上一段继承UserNamePasswordValidator 的class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IdentityModel;
using System.IdentityModel.Tokens;
using System.IdentityModel.Selectors;

public class Service : IService {
    public string GetData(int value) {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite) {
        if (composite == null) {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue) {
            composite.StringValue += "Suffix";
        }
        return composite;
    }

    public class MyUserValidator : UserNamePasswordValidator {
        public override void Validate(string userName, string password) {
            if (null == userName || null == password) {
                throw new ArgumentNullException();
            }

            if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) {
                throw new FaultException("Unknown Username or Incorrect Password");
            }
        }
    }
}


然后修改web.config, 但在customUserNamePasswordValidatorType这里搞不定

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None" />