2

ユーザーごとに 1 つのセッションのみを有効にするコードを実装したいと考えています。フォーム認証でasp.netを使用しています。

この投稿を読みました: ASP.NET でユーザーごとに 1 つのセッションのみに制限する

しかし、以前のセッションをすべて切断するには、反対のことをしたいと思います。

ここに私のコードがあります:

void Application_Start(object sender, EventArgs e) 
{
    Application["UsersLoggedIn"] = new System.Collections.Generic.Dictionary<string, string>(); }

ユーザーがログインすると、レコードを挿入します:ユーザー名、セッションID。ユーザー名が存在する場合は、sessionID を更新するだけです

    void Application_BeginRequest(object sender, EventArgs e)
    {
        System.Collections.Generic.Dictionary<string, string> d = HttpContext.Current.Application["UsersLoggedIn"] as System.Collections.Generic.Dictionary<string, string>;
        if (d != null)
        {
            if (d.Count > 0)
            {
                    string userName = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(userName))
                    {
                        if (d.ContainsKey(userName))
                        {
                            if (d[userName] != HttpContext.Current.Session.SessionID)
                            {
                                FormsAuthentication.SignOut();
                                Session.Abandon();
                            }
                        }
                    }
}
}

しかし、空のHttpContext.Current.Userを取得します。「AuthenticatedRequest」も試しましたが、null Session になります。

アイデアはありますか?

4

2 に答える 2

3

AcquireRequestStateHttpContext.Current.User および Session にアクセスする場合は、イベントを使用する必要があります。

于 2013-10-30T14:50:35.227 に答える
2

複数のサーバーで負荷分散された状況にある場合、何らかの方法でこれを維持したい場合、問題はユーザーロックを解放する必要がある場合に発生します (ユーザーセッションの終了時またはログアウト時)。

于 2013-10-30T15:59:00.177 に答える