您好,登錄后才能下訂單哦!
Asp.net mvc中怎么實(shí)現(xiàn)權(quán)限過(guò)濾和單點(diǎn)登錄,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
1.權(quán)限控制使用controller和 action來(lái)實(shí)現(xiàn),權(quán)限方式有很多種,最近開(kāi)發(fā)項(xiàng)目使用控制控制器方式實(shí)現(xiàn)代碼如下
/// <summary> /// 用戶權(quán)限控制 /// </summary> public class UserAuthorize : AuthorizeAttribute { /// <summary> /// 授權(quán)失敗時(shí)呈現(xiàn)的視圖 /// </summary> public string AuthorizationFailView { get; set; } /// <summary> /// 請(qǐng)求授權(quán)時(shí)執(zhí)行 /// </summary> /// <param name="filterContext">上下文</param> public override void OnAuthorization(AuthorizationContext filterContext) { // 獲取url請(qǐng)求里的 controller 和 action string controllerName = filterContext.RouteData.Values["controller"].ToString(); string actionName = filterContext.RouteData.Values["action"].ToString(); // 獲取用戶信息 UserLoginBaseInfo _userLoginInfo = filterContext.HttpContext.Session[Property.UerLoginSession] as UserLoginBaseInfo; //根據(jù)請(qǐng)求過(guò)來(lái)的controller和action去查詢可以被哪些角色操作: 這是查詢數(shù)據(jù)庫(kù) roleid使用 1,2,3,4格式 RoleWithControllerAction roleWithControllerAction = SampleData.roleWithControllerAndAction.FirstOrDefault(r => r.ControllerName.ToLower() == controllerName.ToLower() && r.ActionName.ToLower() == actionName.ToLower() && r.RoleIds.contails("3")); // 有值處理 if (roleWithControllerAction != null) { //有權(quán)限操作當(dāng)前控制器和Action的角色id this.Roles = roleWithControllerAction.RoleIds; } else { //請(qǐng)求失敗輸出空結(jié)果 filterContext.Result = new EmptyResult(); //打出提示文字 HttpContext.Current.Response.Write("對(duì)不起,你沒(méi)有權(quán)限操作!"); } base.OnAuthorization(filterContext); } /// <summary> /// 自定義授權(quán)檢查(返回False則授權(quán)失?。? /// </summary> protected override bool AuthorizeCore(HttpContextBase httpContext) { //if (httpContext.User.Identity.IsAuthenticated) //{ // string userName = httpContext.User.Identity.Name; //當(dāng)前登錄用戶的用戶名 // User user = SampleData.users.Find(u => u.UserName == userName); //當(dāng)前登錄用戶對(duì)象 // if (user != null) // { // Role role = SampleData.roles.Find(r => r.Id == user.RoleId); //當(dāng)前登錄用戶的角色 // foreach (string roleid in Roles.Split(',')) // { // if (role.Id.ToString() == roleid) // return true; // } // return false; // } // else // return false; //} //else // return false; //進(jìn)入HandleUnauthorizedRequest return true; } /// <summary> /// 處理授權(quán)失敗的HTTP請(qǐng)求 /// </summary> protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { if (string.IsNullOrWhiteSpace(AuthorizationFailView)) AuthorizationFailView = "error"; filterContext.Result = new ViewResult { ViewName = AuthorizationFailView }; } }
二.單點(diǎn)登錄方式使用application方式來(lái)實(shí)現(xiàn)
1.用戶登錄成功后記錄當(dāng)前信息
/// <summary> /// 限制一個(gè)用戶只能登陸一次 /// </summary> /// <returns></returns> private void GetOnline() { string UserID = "1"; Hashtable SingleOnline = (Hashtable)System.Web.HttpContext.Current.Application[Property.Online]; if (SingleOnline == null) SingleOnline = new Hashtable(); IDictionaryEnumerator idE = SingleOnline.GetEnumerator(); string strKey = string.Empty; while (idE.MoveNext()) { if (idE.Value != null && idE.Value.ToString().Equals(UserID)) { //already login strKey = idE.Key.ToString(); //當(dāng)前用戶已存在移除、 SingleOnline.Remove(strKey); System.Web.HttpContext.Current.Application.Lock(); System.Web.HttpContext.Current.Application[Property.Online] = SingleOnline; System.Web.HttpContext.Current.Application.UnLock(); break; } } //SessionID if (!SingleOnline.ContainsKey(Session.SessionID)) { SingleOnline[Session.SessionID] = UserID; System.Web.HttpContext.Current.Application.Lock(); System.Web.HttpContext.Current.Application[Property.Online] = SingleOnline; System.Web.HttpContext.Current.Application.UnLock(); } }
2.使用ActionFilter來(lái)實(shí)現(xiàn)單點(diǎn)登錄,每次點(diǎn)擊控制器都去查詢過(guò)濾是否在其它地方登錄
/// <summary> /// 用戶基礎(chǔ)信息過(guò)濾器 /// </summary> public class LoginActionFilter : ActionFilterAttribute { /// <summary> /// 初始化地址 /// </summary> public const string Url = "~/Login/Index?error="; /// <summary> /// 該方法會(huì)在action方法執(zhí)行之前調(diào)用 /// </summary> /// <param name="filterContext">上下文</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { // 獲取上一級(jí)url // var url1 = filterContext.HttpContext.Request.UrlReferrer; UserLoginBaseInfo _userLogin = filterContext.HttpContext.Session[Property.UerLoginSession] as UserLoginBaseInfo; // 用戶是否登陸 if (_userLogin == null) { filterContext.Result = new RedirectResult(Url + "登陸時(shí)間過(guò)期,請(qǐng)重新登陸!&url=" + filterContext.HttpContext.Request.RawUrl); } else { filterContext.HttpContext.Session.Timeout = 30; } //判斷是否在其它地方登錄 Hashtable singleOnline = (Hashtable)System.Web.HttpContext.Current.Application[Property.Online]; // 判斷當(dāng)前SessionID是否存在 if (singleOnline != null && !singleOnline.ContainsKey(HttpContext.Current.Session.SessionID)) filterContext.Result = new RedirectResult(Url + "你的帳號(hào)已在別處登陸,你被強(qiáng)迫下線!"); base.OnActionExecuting(filterContext); } /// <summary> /// 執(zhí)行后 /// </summary> /// <param name="filterContext"></param> public override void OnResultExecuting(ResultExecutingContext filterContext) { //記錄操作日志,寫進(jìn)操作日志中 var controllerName = filterContext.RouteData.Values["controller"]; var actionName = filterContext.RouteData.Values["action"]; base.OnResultExecuting(filterContext); }
3.用戶正常退出或則非正常退出處理當(dāng)前用戶信息銷毀Session
/// <summary> /// Session銷毀 /// </summary> protected void Session_End() { Hashtable SingleOnline = (Hashtable)Application[Property.Online]; if (SingleOnline != null && SingleOnline[Session.SessionID] != null) { SingleOnline.Remove(Session.SessionID); Application.Lock(); Application[Property.Online] = SingleOnline; Application.UnLock(); } Session.Abandon(); }
關(guān)于Asp.net mvc中怎么實(shí)現(xiàn)權(quán)限過(guò)濾和單點(diǎn)登錄問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。