asp.net 强制性单一登陆现实
本文章使用asp.net内置membership作为登陆操作 关于配置membership 不用说明了 网上都有的首先建立一个login页面 随便放一个login控件和loginstatus控件aspx代码<body><form id="form1" runat="server"><div><asp:Login ID="Login1" runat="server" onloggedin="Login1_LoggedIn" onloggingin="Login1_LoggingIn"></asp:Login><asp:LoginStatus ID="LoginStatus1" runat="server" onloggingout="LoginStatus1_LoggingOut" /></div></form> </body>cs代码 MembershipUser user;protected void Login1_LoggedIn(object sender, EventArgs e){if(user ==null)user = Membership.GetUser(User.Identity.Name);//获取登陆用户名的membershipuser实例 Guid newguid = Guid.NewGuid();//新建guid HttpCookie cookie=Response.Cookies[FormsAuthentication.FormsCookieName];//获取cookie FormsAuthenticationTicket ft = FormsAuthentication.Decrypt(cookie.Value);//解密表单票FormsAuthenticationTicket newft = new FormsAuthenticationTicket(ft.Version, ft.Name, ft.IssueDate, ft.Expiration, ft.IsPersistent, newguid.ToString(), ft.CookiePath);//重新创建一个表单票 把生成guid加入userdata中user.Comment = "loginExpiration;" + ft.Expiration.ToString() + "|loginSessionID;" + newguid.ToString();//存储guid数据和过期时间Membership.UpdateUser(user);//更新用户数据Response.Cookies.Remove(FormsAuthentication.FormsCookieName);//删除已有相关formsName的cookieHttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newft));//重新创建cookienewCookie.Domain = cookie.Domain;newCookie.Expires = cookie.Expires;newCookie.HttpOnly = cookie.HttpOnly;newCookie.Path = cookie.Path;newCookie.Secure = cookie.Secure;Response.Cookies.Add(newCookie);//输出cookie到客户端 }protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e){if (user == null){user = Membership.GetUser(Login1.UserName);}//禁止同一个会话再次登陆//禁止同一个会话再次登陆if (user != null){if (User.Identity.IsAuthenticated && user.UserName == User.Identity.Name){if (!string.IsNullOrEmpty(user.Comment) && user.Comment.Contains("loginExpiration")){string currentExpirationStr = user.Comment.Split("|".ToCharArray())[0];DateTime currentExpiration = DateTime.Parse(currentExpirationStr.Split(";".ToCharArray())[1]);if (currentExpiration < DateTime.Now){e.Cancel = true;Literal t = Login1.FindControl("FailureText") as Literal;t.Text = "你已经登陆了 !";}}}}}protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e){//退出登陆 清空用户的comment数据MembershipUser mu = Membership.GetUser();mu.Comment = string.Empty;Membership.UpdateUser(mu);}然后 需要一个Httpmodule模块cs代码using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security;namespace aspnetajaxtast {public class FormsAuthsessionModule : IHttpModule{public void Dispose(){}public void Init(HttpApplication context){context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest);}void context_PostAuthorizeRequest(object sender, EventArgs e){HttpApplication app = sender as HttpApplication;HttpContext c = app.Context;if (c.User.Identity.IsAuthenticated){FormsAuthenticationTicket ft = (c.User.Identity as FormsIdentity).Ticket;Guid g;if (ft.UserData != ""){g = new Guid(ft.UserData);}elseg = Guid.Empty;MembershipUser user = Membership.GetUser(c.User.Identity.Name);Guid currentSessionGuid;if (!string.IsNullOrEmpty(user.Comment)){string currentSessionStr = user.Comment.Split("|".ToCharArray())[1];currentSessionGuid = new Guid(currentSessionStr.Split(";".ToCharArray())[1]);}else{currentSessionGuid = Guid.Empty;}if (g != currentSessionGuid){FormsAuthentication.SignOut();//清空cookie登陆数据 需要重向url//自己自定义转到url的代码 }}}} }web.config 需要配置httpmodule在<system.web>下<httpModules><add name="FormsAuthsessionModules" type="aspnetajaxtast.FormsAuthsessionModule"/></httpModules>这是vs测试或者iis7以下版本需要的如果在iis7 需要以下配置代码<system.webServer><modules runAllManagedModulesForAllRequests="true" ><add name="FormsAuthsessionModules" type="FormsAuthsessionModule"/></modules></system.webServer>测试需要两个浏览器就可以了 一个ie 一个ff可以当模拟两台电脑 如果你有两台电脑的话 也可以
转载于:https://www.cnblogs.com/wifi/articles/2456516.html
asp.net 强制性单一登陆现实相关推荐
- Ajax+asp.net实现用户登陆 转自http://www.shangxueba.com/jingyan/2933319.html
这篇文章主要介绍了Ajax+asp.net实现用户登陆,主要是为了练习ajax的使用方法,有需要的小伙伴参考下. 以用户登录为例练习ajax的使用方法 login.html <!DOCTYPE ...
- ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要...
ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm -- Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...
- asp.net58同城简单登陆
asp.net58同城简单登陆界面 以asp.net来设计的一个登陆 1.comfing连接自己需要的数据库(自行设计) 2.搭建三层架构 3.界面设计 <%@ Page Language=&q ...
- asp.net中退出登陆的相关问题(解决后退或直接粘贴地址进入网页的问题)
今天在做退出登陆的时候遇到了问题,即使按了退出转到登录页面,但Session还是存在的,当直接粘贴地址进入(不登录)时Session仍然有效,不能做到只有登录了才能进入的效果.在网上搜了一下找到了答案 ...
- asp.net后台管理系统-登陆模块-路由权限控制_1
using System.Web.Routing;//重写System.Web.Routing中Initialize方法protected override void Initialize(Reque ...
- 最简单代码ASP.NET开源QQ登陆for Oauth2.0
使用的是VS2005开发环境,如果要使用VS2008或VS2010,请自行更换附带的Newtonsoft.Json.dll版本. 1.设置web.config 的 app_ID 和 app_Key 2 ...
- MOSS2007 实现单点登陆
经过N天的时间终于把sso搞定了,现在抽出一些时间总结一下.希望对你有所帮助. 第一部分:在管理中心进行配置 1.打开 管理中心à操作 2.单击 管理单一登陆的设置 后进入设置页 ...
- ASP.NET 2.0应用程序安全强化纵览
目前,实现Web开发的核心技术之一是ASP.NET 2.0.这种技术能够帮助企业快速开发出功能强大的Web应用程序,深受开发人员喜爱.然而,在开发人员实现业务逻辑的同时,由于各种原因的影响,应用程序的 ...
- 学习:MOSS2007 实现单点登陆(转)
第一部分:在管理中心进行配置 1.打开 管理中心à操作 2.单击 管理单一登陆的设置 后进入设置页面,如下图 3. 单击"管理服务器设置 1. 单一登录管理员 ...
最新文章
- AngularDart Material Design 输入
- 12款经典图片轮播jquery插件
- 安装mysql5.7.24rpm_centos7安装mysql-5.7.24(rpm安装)
- 数据库 连接池、缓冲池(定义、原理)
- 安卓第一阶段实训项目:基于存储卡音乐播放器(更新中……)
- 10月23日见?疑似魅族16T预热海报曝光:定位大屏娱乐旗舰
- mysql 数据库军规_用尽洪荒之力整理的Mysql数据库32条军规(转)
- 29. Element ownerDocument 属性
- 【重点】commons-dbutils
- 斜齿轮重合度计算公式_齿轮强度校核及重合度计算(已优化)
- 最新CPC客户端安装教程,亲试有效
- 一篇基金研报--《外包服务:后安迪-比尔时代IT产业的大餐》
- 显卡更新后重启计算机就没了,在windows10系统更新显卡后黑屏的解决方法
- gis热力专题图制作
- 转载,近三年知名实验室slam有关论文。
- 反思学习,做更好的自己
- 【SwiftUI模块】0012、SwiftUI-搭建一个类似微博、网易云、抖音个人页面的头部下拉放大图片效果
- windows安装软件出现“command line option syntax error”错误
- 使用Aspose在Java中将Excel文件转换为HTML
- WiFi 破解连接方式