ユーザーごとに 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 になります。
アイデアはありますか?