本文章使用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 强制性单一登陆现实相关推荐

  1. Ajax+asp.net实现用户登陆 转自http://www.shangxueba.com/jingyan/2933319.html

    这篇文章主要介绍了Ajax+asp.net实现用户登陆,主要是为了练习ajax的使用方法,有需要的小伙伴参考下. 以用户登录为例练习ajax的使用方法 login.html <!DOCTYPE ...

  2. ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要...

    ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm --  Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...

  3. asp.net58同城简单登陆

    asp.net58同城简单登陆界面 以asp.net来设计的一个登陆 1.comfing连接自己需要的数据库(自行设计) 2.搭建三层架构 3.界面设计 <%@ Page Language=&q ...

  4. asp.net中退出登陆的相关问题(解决后退或直接粘贴地址进入网页的问题)

    今天在做退出登陆的时候遇到了问题,即使按了退出转到登录页面,但Session还是存在的,当直接粘贴地址进入(不登录)时Session仍然有效,不能做到只有登录了才能进入的效果.在网上搜了一下找到了答案 ...

  5. asp.net后台管理系统-登陆模块-路由权限控制_1

    using System.Web.Routing;//重写System.Web.Routing中Initialize方法protected override void Initialize(Reque ...

  6. 最简单代码ASP.NET开源QQ登陆for Oauth2.0

    使用的是VS2005开发环境,如果要使用VS2008或VS2010,请自行更换附带的Newtonsoft.Json.dll版本. 1.设置web.config 的 app_ID 和 app_Key 2 ...

  7. MOSS2007 实现单点登陆

    经过N天的时间终于把sso搞定了,现在抽出一些时间总结一下.希望对你有所帮助. 第一部分:在管理中心进行配置    1.打开 管理中心à操作        2.单击 管理单一登陆的设置  后进入设置页 ...

  8. ASP.NET 2.0应用程序安全强化纵览

    目前,实现Web开发的核心技术之一是ASP.NET 2.0.这种技术能够帮助企业快速开发出功能强大的Web应用程序,深受开发人员喜爱.然而,在开发人员实现业务逻辑的同时,由于各种原因的影响,应用程序的 ...

  9. 学习:MOSS2007 实现单点登陆(转)

    第一部分:在管理中心进行配置 1.打开 管理中心à操作      2.单击 管理单一登陆的设置 后进入设置页面,如下图        3. 单击"管理服务器设置     1. 单一登录管理员 ...

最新文章

  1. AngularDart Material Design 输入
  2. 12款经典图片轮播jquery插件
  3. 安装mysql5.7.24rpm_centos7安装mysql-5.7.24(rpm安装)
  4. 数据库 连接池、缓冲池(定义、原理)
  5. 安卓第一阶段实训项目:基于存储卡音乐播放器(更新中……)
  6. 10月23日见?疑似魅族16T预热海报曝光:定位大屏娱乐旗舰
  7. mysql 数据库军规_用尽洪荒之力整理的Mysql数据库32条军规(转)
  8. 29. Element ownerDocument 属性
  9. 【重点】commons-dbutils
  10. 斜齿轮重合度计算公式_齿轮强度校核及重合度计算(已优化)
  11. 最新CPC客户端安装教程,亲试有效
  12. 一篇基金研报--《外包服务:后安迪-比尔时代IT产业的大餐》
  13. 显卡更新后重启计算机就没了,在windows10系统更新显卡后黑屏的解决方法
  14. gis热力专题图制作
  15. 转载,近三年知名实验室slam有关论文。
  16. 反思学习,做更好的自己
  17. 【SwiftUI模块】0012、SwiftUI-搭建一个类似微博、网易云、抖音个人页面的头部下拉放大图片效果
  18. windows安装软件出现“command line option syntax error”错误
  19. 使用Aspose在Java中将Excel文件转换为HTML
  20. WiFi 破解连接方式

热门文章

  1. OpenCV-Python 中文教程(搬运)目录
  2. (转)C# WebApi 身份认证解决方案:Basic基础认证
  3. Dockerfile构建容器镜像 - 运维笔记
  4. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
  5. 在UltraEdit的查找和替换中使用正则表达式 (转)
  6. An ffmpeg and SDL Tutorial
  7. matlab中gatbx工具箱的添加
  8. Heka:Go编写,来自Mozilla,高效、灵活的插件式数据挖掘工具(转)
  9. Java GUI 开发专题
  10. Ansible基本命令