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");
}
}
}
}
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="None" />
</clientCertificate>
<serviceCertificate findValue="HTTPS-Server" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Service.MyUserValidator,Service" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
namespace WCFWebHost {
public class Service : IService {
...........
...........
}
}