asp.net 实现同一账户不能同时多处登录(单点登录)

Web 项目中,经常会遇到同一账户不能多处同时登录的问题,相应的解决办法也很多,总结出来也就一下几点:

  1. 将登录后的用户名放到数据库表中;
  2. 登录后的用户名放到Session中;
  3. 登录后的用户名放到Application中;
  4. 登录后的用户名放到Cache中。

一般的这几种方法都是登录了之后,如果没有正常退出,第二次登录将不被允许。这样一般都会存在一个问题:如果用户没有正常退出系统,那么他接下来继续登录的时候,因为Session没有过期等问题,会被拒绝继续登录系统,只能等待Session过期后才能登录。本文介绍的方法是采用类似于MSN登陆的方法,第二次登录时会把第一次的登录注销掉,第一次登录将会类似于MSN弹出:您的帐号已在别处被登录,您被强迫下线的提示信息。
功能实现起来也比较简单:
登录用户名密码验证通过之后输入以下代码:

Hashtable portal_hOnline = (Hashtable)HttpContext.Current.Application["Online"];
 if (portal_hOnline != null)
 {
     IDictionaryEnumerator idE = portal_hOnline.GetEnumerator();
     string strKey = "";
     while (idE.MoveNext())
     {
         if (idE.Value != null && idE.Value.ToString().Equals(username))
         {
             //already login
             strKey = idE.Key.ToString();
             portal_hOnline[strKey] = "XXXXX";
             break;
         }
     }
 }
 else
 {
     portal_hOnline = new Hashtable();
 }

 portal_hOnline[HttpContext.Current.Session.SessionID] = username;
 HttpContext.Current.Application.Lock();
 HttpContext.Current.Application["Online"] = portal_hOnline;
 HttpContext.Current.Application.UnLock();

用户登录的时候将登录用户名放在一个全局变量Online,Online为Hashtable结构,Key为SessionID,Value为用户名。每次用户登录时均判断以下要登录的用户名在Online中是不是已经存在,如果存在该用户名已经被登录,将第一个人登录的SessionID对应的用户名强制变更为XXXXXX,表示该登录将被强制注销。
建立一个CommonPage页,系统中所有的页面都继承于CommonPage页,在CommonPage页的后台代码中添加如下代码:

Hashtable hOnline = (Hashtable)HttpContext.Current.Application["Online"];
 if (hOnline != null)
 {
     IDictionaryEnumerator idE = hOnline.GetEnumerator();
     while (idE.MoveNext())
     {
         if (idE.Key != null && idE.Key.ToString().Equals(HttpContext.Current.Session.SessionID))
         {
             //olready login
             if (idE.Value != null && "XXXXX".ToString().Equals(idE.Value.ToString()))
             {
                 hOnline.Remove(HttpContext.Current.Session.SessionID);
                 HttpContext.Current.Application.Lock();
                 HttpContext.Current.Application["Online"] = hOnline;
                 HttpContext.Current.Application.UnLock();
                 string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
                 HttpContext.Current.Response.Write(string.Format(js, "帐号已在别处登录 ,你将被强迫下线(请保管好自己的用户密码)!", "logout.aspx?cname=noadmin"));
                 return;
             }
             break;
         }
     }
 }

继承于CommonPage的页面在刷新时都要执行重载的OnInit中的代码,取出Online,找到该用户对应的SessionID,判断SessionID里对应的用户名是否变更,如果变更,就强迫下线,清掉Session,转到Login画面。
最后需要在Session过期或者退出系统时释放资源,在Global.asax文件中的Session_End中添加如下代码:

Hashtable hOnline =(Hashtable)Application["Online"];
if (hOnline[Session.SessionID] != null)
{
    hOnline.Remove(Session.SessionID);
    Application.Lock();
    Application["Online"] = hOnline;
    Application.UnLock();
}

如果用户不正常退出后重登录,因为重登录的优先级大,不会影响用户的登录,而不正常退出的用户占用的资源会在Session过期后自动清除,不会影响系统的性能。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core中实现单点登录需要进行以下步骤: 1. 配置身份提供者中的客户端信息,包括客户端ID、客户端密钥、回调URL等信息。 2. 在应用中配置身份认证和单点登录,可以使用OpenID Connect等协议来实现。 3. 在需要使用单点登录的Action方法中使用[Authorize]特性进行身份认证。 4. 可以使用ASP.NET Core Identity和IdentityServer4等框架来实现单点注销。 下面是一个简单的示例,演示如何在.NET Core中实现单点登录: ```csharp // Startup.cs public void ConfigureServices(IServiceCollection services) { // 配置身份认证 services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) .AddCookie() .AddOpenIdConnect(options => { // 配置OpenID Connect options.Authority = "https://accounts.example.com"; options.ClientId = "client_id"; options.ClientSecret = "client_secret"; options.ResponseType = "code"; options.Scope.Add("openid"); options.Scope.Add("profile"); options.CallbackPath = "/signin-oidc"; options.SaveTokens = true; }); services.AddControllersWithViews(); } // HomeController.cs public class HomeController : Controller { [Authorize] public IActionResult Index() { return View(); } } // LogoutController.cs public class LogoutController : Controller { public async Task<IActionResult> Index() { // 注销当前用户 await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); // 跳转到注销页面 return Redirect("https://accounts.example.com/logout"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值