asp.net 使用 Application 限制单一登录

原理:用户登录后系统会分配一个与用户唯一对应的SessionID,将当前用户ID与其SessionID对应保存在Application中,一旦该用户在其他地方重复登录则Application中保存的SessionID就会被更新,导致当前session中的SessionID与Application中的SessionID不再一致

用户登录后保存SessionID在Application中

private static void RecordLogin(string strUId)
{
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["SESSIONID_" + strUId] = HttpContext.Current.Session.SessionID;
HttpContext.Current.Application.UnLock();
}

判断方法

public static bool CheckRepeatLogin(string strUId)
{
object objSessionId = HttpContext.Current.Application["SESSIONID_" + strUId];
if (objSessionId == null || objSessionId.ToString() == "") return false; return objSessionId.ToString() != HttpContext.Current.Session.SessionID;
}

aspx页面跳转时判断:添加基类 BasePage.cs

public class BasePage:System.Web.UI.Page
{
public UserInfo CurUser = null; protected override void OnInitComplete(EventArgs e)
{
CurUser = CurSession.CurUser; if (CurUser == null)
{
Response.Redirect(SysHelper.GetVirtualPath() + "pagesessionnull.html", true);
} if (LoginService.CheckRepeatLogin(CurUser.UId))
{
Response.Redirect(SysHelper.GetVirtualPath() + "pagerepeatlogin.html", true);
} base.OnInitComplete(e);
} protected override void OnLoadComplete(EventArgs e)
{
Response.Cache.SetNoStore();
base.OnLoadComplete(e);
}
}

ashx页面请求时判断:添加基类 BaseHandler.cs

public class BaseHandler : IHttpHandler, IRequiresSessionState
{
public UserInfo CurUser = null;
public HttpContext CurContext = null; public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); try
{
CurUser = CurSession.CurUser;
CurContext = context; if (CurUser == null)
{
context.Response.Write(JsonHelper.GetResult(false, "登录超时,请重新登录", new { rcode = - }));
}
else if (LoginService.CheckRepeatLogin(CurUser.UId))
{
context.Response.Write(JsonHelper.GetResult(false, "您的帐号在其他地方登录,您已经被踢出,请重新登录", new { rcode = - }));
}
else
{
context.Response.Write(ActionMethod());
}
}
catch (Exception ex)
{
context.Response.Write(JsonHelper.GetResult(ex.Message.ToString()));
}
finally
{
context.Response.End();
}
} public virtual string ActionMethod()
{
return JsonHelper.GetResult();
}
public bool IsReusable
{
get
{
return false;
}
}
}

asp.net单一登录的更多相关文章

  1. MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN

    在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...

  2. [转]MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN

    本文转自:http://www.cnblogs.com/jesse2013/p/aspnet-identity-claims-based-authentication-and-owin.html 在M ...

  3. MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN -摘自网络

    在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...

  4. (转)ASP.NET Identity登录原理 - Claims-based认证和OWIN

    在Membership系列的最后一篇引入了ASP.NET  Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详 ...

  5. ASP.NET Identity登录原理 - Claims-based认证和OWIN

    MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN 在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣 ...

  6. ASP.NET中登录时记住用户名和密码(附源码下载)--ASP.NET

    必需了解的:实例需要做的是Cookie对象的创建和对Cookie对象数据的读取,通过Response对象的Cookies属性创建Cookie,通过Request对象的Cookies可以读取Cookie ...

  7. ASP.NET Core 登录失败。该登录名来自不受信任的域,不能与集成身份验证一起使用。

    原文:ASP.NET Core 登录失败.该登录名来自不受信任的域,不能与集成身份验证一起使用. 当进行数据迁移的时候提示 修改appsettings配置连接串的Trusted_Connection ...

  8. ASP.NET中登录功能的简单逻辑设计

     ASP.NET中登录功能的简单逻辑设计                               概述                               逻辑设计             ...

  9. 【Azure Active Directory】单一登录 (SAML 协议)

    Azure Active Directory 支持 SAML 2.0 Web 浏览器单一登录 (SSO) 配置文件. 若要请求 Azure Active Directory 对用户进行身份验证时,云服 ...

随机推荐

  1. OpenCV-3.4.3图像通道处理

    图像通道处理 图像读取和处理都是按BGR通道顺序进行的 #include <iostream> #include <opencv2/opencv.hpp> #include & ...

  2. shadertoy使用教程

    shadertoy使用教程 /** *常量定义 */ uniform vec3 iResolution; // 窗口分辨率,单位像素 uniform float iTime; // 程序运行的时间,单 ...

  3. Java描述设计模式(12):外观模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 在移动互联网没有普及之前,去饭店吃饭的流程大致如下:选座位,排队,点菜,结账.后来移动互联网普及,通过手机APP就 ...

  4. Java题库——Chapter6 一维数组

    1)What is the representation of the third element in an array called a? 1) _______ A)a(3) B) a(2) C) ...

  5. Python实现变声器功能,萝莉音御姐音都有的哦

    登录百度AL开发平台 在控制台选择语音合成 创建应用 填写应用信息 在应用列表获取(Appid.API Key.Secret Key) 6. 安装pythonsdk 安装使用Python SDK有如下 ...

  6. Use a Multiline Editor for String Properties 对字符串属性使用多行编辑器

    In this lesson, you will learn how to display a multiline editor for string properties. For this pur ...

  7. Assign a Custom Image 设置自定义图标

    In this lesson, you will learn how to associate a business class with a custom image. This image wil ...

  8. selenium时间等待方法

    在UI自动化测试中,必然会遇到环境不稳定.网络慢等情况.当你觉得定位没有问题,但程序却直接报了元素不可见时,那你就需要思考是否因为程序运行太快或者页面加载太慢而造成了元素不可见,必须要再等待直至元素可 ...

  9. centos python3 的 卸载 删除

    卸载/删除python 3.4看到网上说慎用 apt-get remove和 yum remove ,因此不敢用此类命令用卸载了(以后阿里云服务器快过期不用了的时候可以试一下,看看系统是否会崩,哈哈) ...

  10. go语言设计模式之proxy

    代理模式,单元测试用例真的写得详细, 受教~ proxy.go package proxy import ( //"errors" "fmt" ) type U ...