public void SignIn(User user, bool createPersistentCookie)
        {
            var now = DateTime.Now.ToLocalTime();

            var ticket = new FormsAuthenticationTicket(
                1 /*version*/,
                user.Email,
                now,
                now.Add(ExpirationTimeSpan),
                createPersistentCookie,
                user.Email,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }

            httpContext.Response.Cookies.Add(cookie);
            cachedUser = user;
        }
        public User GetAuthenticatedUser()
        {
            if (cachedUser != null)
                return cachedUser;

            if (httpContext == null ||
                httpContext.Request == null ||
                !httpContext.Request.IsAuthenticated ||
                !(httpContext.User.Identity is FormsIdentity))
            {
                return null;
            }

            var formsIdentity = (FormsIdentity)httpContext.User.Identity;
            var usernameOrEmail = formsIdentity.Ticket.UserData;

            if (String.IsNullOrWhiteSpace(usernameOrEmail))
                return null;
            var user = userService.GetUserByEmail(usernameOrEmail);
            if (user != null && user.IsApproved)
                cachedUser = user;
            return cachedUser;
        }
示例#3
0
 public void SetUsername(User user, string newUsername)
 {
     throw new NotImplementedException();
 }
 public void SignOut()
 {
     cachedUser = null;
     FormsAuthentication.SignOut();
 }
示例#5
0
 public void SetEmail(User user, string newEmail)
 {
     throw new NotImplementedException();
 }