Ejemplo n.º 1
0
        public Identity(Cookie user)
        {
            if (user == null)
            {
                Email = "Guest";
                return;
            }

            Id = user.Id;
            Email = user.Email;
        }
        public void SignIn(IIdentity identity, bool createPersistentCookie)
        {
            var user = identity as Identity;
            if (user == null)
                throw new ArgumentNullException("user");

            var cookie = new Cookie
            {
                Id = user.Id,
                Email = user.Email,
                RememberMe = createPersistentCookie
            };

            string userData = JsonConvert.SerializeObject(cookie);
            var ticket = new FormsAuthenticationTicket(1, cookie.Email, DateTime.Now,
                                                       DateTime.Now.Add(FormsAuthentication.Timeout),
                                                       createPersistentCookie, userData);
            string encTicket = FormsAuthentication.Encrypt(ticket);
            var httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) { Expires = DateTime.Now.Add(FormsAuthentication.Timeout) };

            _httpContext.Response.Cookies.Add(httpCookie);
        }