我目前在 WSE 中验证用户名和密码(令牌)的方法如下:
public override void VerifyToken(SecurityToken token1)
{
if (token1 is UsernameToken)
{
string u1 = (token1 as UsernameToken).Username;
string p1 = (token1 as UsernameToken).Password;
// see if this user is already authenticated
UsernameToken token2 = TokenCache[u1] as UsernameToken;
if ((token2 != null) && token2.IsCurrent && (token2.Password == p1))
{
// less than 30s?
if ((DateTime.Now - token2.Created) < TimeSpan.FromSeconds(30))
return;
else
// no - remove from cache
RemoveFromCache(token1);
}
// not cached, so actually check
// NB Two or more requests for same user at about same time may all fail test above
// and thus all will call ValidateUser. But then they will all call CacheSecurityToken below,
// which is OK - the last one wins.
if (Membership.ValidateUser(u1, p1))
{
Cache(token1);
return;
}
// not authenticated
throw new Exception("Authentication failed for " + u1);
}
知道如何在 WCF 中进行更改吗?我使用过Microsoft.Web.Service3 WSE 程序集 - 不想使用它。想从我的整个解决方案中摆脱 UsernameToken 。