首页 > 其他 > 详细

FormAuthentication Stuff

时间:2014-03-28 19:04:17      阅读:470      评论:0      收藏:0      [点我收藏+]
public void SetAuthCookie(int memberId, string accountType)
        {
            var newTicket = new FormsAuthenticationTicket(1,
                memberId.ToString(),
                DateTime.Now,
                DateTime.Now.AddMinutes(30),
                false,
                accountType,
                FormsAuthentication.FormsCookiePath);
            string encTicket = FormsAuthentication.Encrypt(newTicket);
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            HttpContext.Current.Response.Cookies.Add(cookie);

        }


"UserData"这里为 accountType. 不能为Null 否则 Encrypt的时候会为null.


//Global.asax

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                if (authTicket != null && authTicket.UserData != null)
                {
                    var rehauIdentity = new RehauIdentity(HttpContext.Current.User.Identity) {AccountType = authTicket.UserData};
                    HttpContext.Current.User = rehauIdentity;
                }
            }
        }

//IdentityClass

public class RehauIdentity : IPrincipal
    {
        public IIdentity Identity { get; private set; }
        public bool IsInRole(string role) { return Roles.IsUserInRole(Identity.Name, role); }
        public RehauIdentity(IIdentity identity) { this.Identity = identity; }
        public string AccountType { set; get; }
    }



FormAuthentication Stuff,布布扣,bubuko.com

FormAuthentication Stuff

原文:http://blog.csdn.net/lglgsy456/article/details/22377409

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!