2

サービス側でユーザー名とパスワードのトークンをキャッシュする適切な方法を見つけようとしていますので、サービスへの接続が確立されるたびにデータベース接続を作成する必要はありません。

これは私が達成しようとしているものです:

public class ServiceAuth : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        var user = Repository.Authenticate(userName, password);

        if (user != null)
        {
            // Perform some secure caching
        }
        else
            throw new FaultException("Login Failed");
    }
}

UserNamePasswordValidator を使用して C# 4.0 WCF で資格情報を検証するときにキャッシュを使用することは可能ですか?

もしそうなら、誰かがこれを達成する方法についての手がかりを教えてもらえますか?

4

1 に答える 1

2

スーパーユーザーには、問題の解決策を見つけたいと思っている他の人に役立つ可能性があるため、回答を削除しないようにお願いしたいと思います。

キャッシュにキーと値のペアのディクショナリコレクションを使用して、次のCUSTOMセキュリティマネージャーを実装しました。お役に立てれば

public class SecurityManager : UserNamePasswordValidator
{
    //cacheCredentials stores username and password
    static Dictionary<string, string> cacheCredentials = new Dictionary<string, string>();
    //cacheTimes stores username and time that username added to dictionary.
    static Dictionary<string, DateTime> cacheTimes = new Dictionary<string, DateTime>();

    public override void Validate(string userName, string password)
    {
        if (userName == null || password == null)
        {
            throw new ArgumentNullException();
        }
        if (cacheCredentials.ContainsKey(userName))
        {
            if ((cacheCredentials[userName] == password) && ((DateTime.Now - cacheTimes[userName]) < TimeSpan.FromSeconds(30)))// &&  timespan < 30 sec - TODO
                return;
            else
                cacheCredentials.Clear();
        }
        if (Membership.ValidateUser(userName, password))
        {
            //cache usename(key) and password(value)
            cacheCredentials.Add(userName, password);
            //cache username(key), time that username added to dictionary 
            cacheTimes.Add(userName, DateTime.Now);
            return;
        }
        throw new FaultException("Authentication failed for the user");       
    }
}
于 2013-02-05T17:26:40.800 に答える