public void LoginUser(User user, bool persistent) { var ticket = new FormsAuthenticationTicket(1, user.Id.ToString(), DateTime.Now, DateTime.Now.AddMinutes(30), persistent, _serializer.Serialize(user.ToJson())); var encryptedTicket = FormsAuthentication.Encrypt(ticket); _context.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)); }
public IEnumerable<ServiceError> CreateUser(User user, string password, Organization organization) { var errors = new List<ServiceError>(); if (user == null) { errors.Add(new ServiceError(String.Empty, "User cannot be null.")); return errors; } if (String.IsNullOrEmpty(password)) { errors.Add(new ServiceError("Password", "Password cannot be empty.")); } if (!IsEmailAddressUnique(user.Email)) { errors.Add(new ServiceError("Email", "Sorry, that username is taken.")); } if (organization != null && !String.IsNullOrEmpty(organization.Name)) { if (OrganizationExists(organization.Name)) { errors.Add(new ServiceError("OrganizationName", "Sorry, an organization by that name has already registered.")); } } if (!errors.Any()) { if (organization != null && !String.IsNullOrEmpty(organization.Name)) { //save the organization if (Repository.Insert<Organization>(organization)) { user.OrganizationId = organization.Id; } else { errors.Add(new ServiceError(String.Empty, "Sorry, an error occurred while saving the organization.")); } } if (!errors.Any()) { user.PasswordSalt = GenerateSalt(); user.PasswordHash = HashPassword(password, user.PasswordSalt); user.DateCreated = DateTime.UtcNow; Repository.Insert<User>(user); } } return errors; }