基於ASP.MVC票據FormsAuthenticationTicket身份認證

來源:互聯網
上載者:User

標籤:time   encrypt   失效   for   業務需求   asp   過程   ide   dir   

做一個最基礎的業務需求使用者登入,將此使用者的身份發回到用戶端的Cookie,之後此使用者再訪問這個web應用就會連同這個身份Cookie一起發送到服務端。服務端上的授權設定就可以根據不同目錄對不同使用者的訪問授權進行控制了。

1.情境

當使用者登入成功將資訊寫入Cookie添加全域靜態變數,跳轉至主介面如果使用者認證資訊失效就重新跳轉至登入介面

2.代碼

2.1 設定Cookie

  /// <summary>        /// 登入驗證        /// </summary>        /// <returns></returns>        [HttpPost]        [AllowAnonymous]        public ActionResult SetCookie(string loginName, string pwd)        {            AdminLoginInfo admin = new AdminLoginInfo();            admin.LoginName = loginName;            admin.Pwd = pwd;                       //資料放入ticket                   FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "login", DateTime.Now, DateTime.Now.AddMinutes(60), false,  Newtonsoft.Json.JsonConvert.SerializeObject(admin));            //資料加密                   string enyTicket = FormsAuthentication.Encrypt(ticket);                  HttpCookie cookie = new HttpCookie(ticket.Name, enyTicket);            if (ticket.IsPersistent)            {                cookie.Expires = ticket.Expiration;            }            Response.Cookies.Add(cookie);            return this.Json(new { success = true, msg = "成功" });                   }  

2.2 控制器基類BaseController

 [AuthorizeAdmin]    public class BaseController : Controller    {        protected AdminLoginInfo info = AdminLoginInfo.get();    }

2.3 AuthorizeAdmin自訂許可權認證

繼承AuthorizeAttribute並且重寫

在過程請求授權時調用  處理未能授權的HTTP請求

 

 public class AuthorizeAdmin : AuthorizeAttribute    {            protected override bool AuthorizeCore(HttpContextBase httpContext)        {            AdminLoginInfo info = AdminLoginInfo.get();            bool Pass = false;            if (info == null)            {                httpContext.Response.StatusCode = 401;                Pass = false;            }            else            {                Pass = true;            }            return Pass;        }               protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)        {            base.HandleUnauthorizedRequest(filterContext);            filterContext.HttpContext.Response.Write(filterContext.HttpContext.Response.StatusCode);            if (filterContext.HttpContext.Response.StatusCode == 401)            {                filterContext.Result = new RedirectResult("/Home/Login");            }        }    }

2.4全域使用者登入資訊

   public class AdminLoginInfo    {        public static AdminLoginInfo get()        {            HttpCookie cook = HttpContext.Current.Request.Cookies["login"];            if (cook == null)            {                return null;            }            else if (cook.Value == null)            {                return null;            }            AdminLoginInfo mode = null;            try            {                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cook.Value);                mode = Newtonsoft.Json.JsonConvert.DeserializeObject<AdminLoginInfo>(authTicket.UserData);                //經銷商類比登陸                return mode;            }            catch            {                return null;            }                }        public string LoginName { get; set; }        public string Pwd { get; set; }    }

 

基於ASP.MVC票據FormsAuthenticationTicket身份認證

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.