Exemplo n.º 1
0
        public static FormsAuthenticationTicket CreateTicket(mUser user, IList<mRole> roles, IList<mAuth> auths)
        {
            var userInfo = new UserIdentity {
                Id = user.Id.ToString(),
                Name = user.Name,
                Email = user.Id.ToString(),
                Roles = String.Join(",", roles),
                Auths = String.Join(",", auths)
            };

            var ticket = new FormsAuthenticationTicket(1,
                user.Name,
                DateTime.Now,
                DateTime.Now.Add(FormsAuthentication.Timeout),
                false,
                userInfo.ToString());

            return ticket;
        }
Exemplo n.º 2
0
 //MvcApplication:
 //public override void Init() {
 //    this.PostAuthenticateRequest += SecurityExtensions.PostAuthenticateRequestHandler;
 //    base.Init();
 //}
 public static void PostAuthenticateRequestHandler(object sender, EventArgs e)
 {
     HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
     if (authCookie != null && !String.IsNullOrEmpty(authCookie.Value)) {
         var formsAuthentication = DependencyResolver.Current.GetService<IFormsAuthentication>();
         var ticket = formsAuthentication.Decrypt(authCookie.Value);
         var userIde = new UserIdentity(ticket);
         string[] userRoles = userIde.Roles.Split(',');
         HttpContext.Current.User = new GenericPrincipal(userIde, userRoles);
         formsAuthentication.SetAuthCookie(HttpContext.Current, ticket);
     }
 }