當前位置:
首頁 >
asp.net 强制性单一登陆现实
發布時間:2023/12/10
36
豆豆
生活随笔
收集整理的這篇文章主要介紹了
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 强制性单一登陆现实的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PHP】伪静态 - 1. 使用正则表达
- 下一篇: ASP.NET站点跨子域名单点登陆(SS