Exemplo n.º 1
0
        public static FormsAuthenticationCookie ConvertTicketToCookie(AuthenticationTicket ticket)
        {
            var claimsIdentity = (ClaimsIdentity)ticket.Principal.Identity;

            var cookie = new FormsAuthenticationCookie()
            {
                CookiePath   = "",
                ExpiresUtc   = (ticket.Properties.ExpiresUtc ?? DateTime.Now).DateTime.ToUniversalTime(),
                IsPersistent = ticket.Properties.IsPersistent,
                IssuedUtc    = (ticket.Properties.IssuedUtc ?? DateTime.Now).DateTime.ToUniversalTime(),
                UserName     = ticket.Principal.Identity.Name,
                UserData     = null
            };

            return(cookie);
        }
Exemplo n.º 2
0
        public static AuthenticationTicket ConvertCookieToTicket(FormsAuthenticationCookie cookie)
        {
            var authenticationProperties = new AuthenticationProperties()
            {
                AllowRefresh = true,
                ExpiresUtc   = cookie.ExpiresUtc,
                IsPersistent = cookie.IsPersistent,
                IssuedUtc    = cookie.IssuedUtc
            };

            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            identity.AddClaim(new Claim(ClaimTypes.Name, cookie.UserName));

            // Connect to database to get the roles and add them to the identity.
            // Or if the roles are stored in the cookie read the cookie.UserData and parse that into role claims

            var principal = new ClaimsPrincipal(identity);

            return(new AuthenticationTicket(principal, authenticationProperties, CookieAuthenticationDefaults.AuthenticationScheme));
        }