日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

asp.net 强制性单一登陆现实

發(fā)布時(shí)間:2023/12/10 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net 强制性单一登陆现实 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文章使用asp.net內(nèi)置membership作為登陸操作 關(guān)于配置membership 不用說(shuō)明了 網(wǎng)上都有的首先建立一個(gè)login頁(yè)面 隨便放一個(gè)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實(shí)例 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);//重新創(chuàng)建一個(gè)表單票 把生成guid加入userdata中user.Comment = "loginExpiration;" + ft.Expiration.ToString() + "|loginSessionID;" + newguid.ToString();//存儲(chǔ)guid數(shù)據(jù)和過期時(shí)間Membership.UpdateUser(user);//更新用戶數(shù)據(jù)Response.Cookies.Remove(FormsAuthentication.FormsCookieName);//刪除已有相關(guān)formsName的cookieHttpCookie newCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newft));//重新創(chuàng)建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);}//禁止同一個(gè)會(huì)話再次登陸//禁止同一個(gè)會(huì)話再次登陸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 = "你已經(jīng)登陸了 !";}}}}}protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e){//退出登陸 清空用戶的comment數(shù)據(jù)MembershipUser mu = Membership.GetUser();mu.Comment = string.Empty;Membership.UpdateUser(mu);}然后 需要一個(gè)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登陸數(shù)據(jù) 需要重向url//自己自定義轉(zhuǎn)到url的代碼 }}}} }web.config 需要配置httpmodule在<system.web><httpModules><add name="FormsAuthsessionModules" type="aspnetajaxtast.FormsAuthsessionModule"/></httpModules>這是vs測(cè)試或者iis7以下版本需要的如果在iis7 需要以下配置代碼<system.webServer><modules runAllManagedModulesForAllRequests="true" ><add name="FormsAuthsessionModules" type="FormsAuthsessionModule"/></modules></system.webServer>測(cè)試需要兩個(gè)瀏覽器就可以了 一個(gè)ie 一個(gè)ff可以當(dāng)模擬兩臺(tái)電腦 如果你有兩臺(tái)電腦的話 也可以

?

轉(zhuǎn)載于:https://www.cnblogs.com/wifi/articles/2456516.html

總結(jié)

以上是生活随笔為你收集整理的asp.net 强制性单一登陆现实的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。