• MVC模式下具体实现登入笔记


    1.mvc添加登入验证

       public class RequireLoginAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                //redirect if not authenticated
                if (IdentityUser.UserId<=0)
                {
                    //use the current url for the redirect
                    string redirectOnSuccess =Uri.EscapeUriString(filterContext.HttpContext.Request.RawUrl);

                    //send them off to the login page
                    string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
                    string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
                    filterContext.HttpContext.Response.Redirect(loginUrl, true);
                }
            }
        }

      2.把登入信息填好

      private void FillUserInfo(UserInfo etUserInfo)
            {
                Session.Clear();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, etUserInfo.UserName,DateTime.Now, DateTime.Now.AddMinutes(WebConfiguration.GetSessionStateTimeout.Minutes), false, etUserInfo.Id + ";"+etUserInfo.PassWord+";", FormsAuthentication.FormsCookiePath);
                string hash = FormsAuthentication.Encrypt(ticket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); //Hashed ticket
                Response.Cookies.Add(authCookie);
                //FormsAuthentication.SetAuthCookie(etUserInfo.UserName, false, authCookie.Path);
            }

    3.再在需登入的操作前加 [RequireLogin]如

            [RequireLogin]
            [Transaction]
            [AcceptVerbs(HttpVerbs.Post)]
            public void Delete()

    {

    }

    这样就可以实现登入限制的功能了

  • 相关阅读:
    HAL驱动的串口编程陷阱
    ST推出新软件STM32Cube ,让STM32微控制器应用设计变得更容易、更快、更好用
    零宽断言 -- Lookahead/Lookahead Positive/Negative
    Eclipse 正则表达式 查找与替换
    MSDN WinUSB Example
    培训SQLServer 嵌套事务PPT分享
    SQL Server数据库备份的镜像
    禁用nested loop join里的spool
    日常办公工具利器 notepad++
    Change the Target Recovery Time of a Database (SQL Server) 间接-checkpoints flushcache flushcache-message
  • 原文地址:https://www.cnblogs.com/scottpei/p/1597238.html
Copyright © 2020-2023  润新知