Пример #1
0
        public void SignIn(User user, bool createPersistentCookie)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var cookie = new IrisCookie
            {
                UserName   = user.UserName,
                RememberMe = createPersistentCookie,
                Roles      = new List <string> {
                    user.Role.Name ?? "user"
                }
            };

            string userData = JsonConvert.SerializeObject(cookie);
            var    ticket   = new FormsAuthenticationTicket(1, cookie.UserName, 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);
        }
        public void SignIn(IrisUser user, bool createPersistentCookie)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            var cookie = new IrisCookie
            {
                UserName = user.UserName,
                RememberMe = createPersistentCookie,
                Roles = new List<string> { user.Role.Name ?? "user" }
            };

            var userData = JsonConvert.SerializeObject(cookie);

            var ticket = new FormsAuthenticationTicket(1, cookie.UserName, DateTime.Now,
                                                       DateTime.Now.Add(FormsAuthentication.Timeout),
                                                       createPersistentCookie, userData);

            var encTicket = FormsAuthentication.Encrypt(ticket);

            var httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
            {
                HttpOnly = true,
            };

            if (createPersistentCookie)
            {
                httpCookie.Expires = DateTime.Now.Add(FormsAuthentication.Timeout);
            }

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