private bool KeyMatch(User user, string key) { if (user == null) return false; return user.Password == key || SecurityManager.GetHashString(user.FacebookLogin) == key || SecurityManager.GetHashString(user.GoogleLogin) == key || SecurityManager.GetHashString(user.VkLogin) == key; }
public User Authenticate(AuthenticationProvider provider, string id, string name, string lastName, string image) { User user; bool isNewUser = false; switch (provider) { case AuthenticationProvider.Facebook: user = Data.Users.FirstOrDefault(x => x.FacebookLogin == id); if (user == null) { isNewUser = true; user = new User(); } user.FacebookLogin = id; break; case AuthenticationProvider.Google: user = Data.Users.FirstOrDefault(x => x.GoogleLogin == id); if (user == null) { isNewUser = true; user = new User(); } user.GoogleLogin = id; break; case AuthenticationProvider.Vk: user = Data.Users.FirstOrDefault(x => x.VkLogin == id); if (user == null) { isNewUser = true; user = new User(); } user.VkLogin = id; break; default: return null; } user.Name = name; user.LastName = lastName; user.Image = image; if (isNewUser) Data.Users.Add(user); Data.SaveChanges(); return user; }
private void SetUser(User user, string hashedKey) { if (user == null) return; var cookieUser = new HttpCookie("UserId") { Value = Convert.ToString(user.Id), Expires = DateTime.MaxValue }; var cookieKey = new HttpCookie("Key") { Value = hashedKey, Expires = DateTime.MaxValue }; HttpContext.Response.Cookies.Remove("UserId"); HttpContext.Response.Cookies.Remove("Key"); HttpContext.Response.SetCookie(cookieUser); HttpContext.Response.SetCookie(cookieKey); }
private void SendConfirmationMail(HttpContextBase context, User user, Culture culture = Culture.En) { var confirmationMessageSender = new ConfirmationMailSender(); string token = SecurityManager.GetHashString(user.Email + user.Password); if (context.Request.Url != null) { string path = context.Request.Url.GetLeftPart(UriPartial.Authority) + "/User/Confirm?hash=" + token; string message = String.Format(StaticSettings.ConfirmationMessage(culture) + "{0}", path); confirmationMessageSender.Send(StaticSettings.ConfirmationTitle(culture), message, user.Email); } }
public ProcessResult RegistrateUser(HttpContextBase context, RegistrationModel registrationModel, HttpServerUtilityBase server, HttpPostedFileBase imageUpload,Culture culture =Culture.En) { User existingUser = Find(registrationModel.Email,false); if (existingUser != null) return ProcessResults.UserWithSuchEmailExists; var user = new User { Name = registrationModel.Name, LastName = registrationModel.LastName, Email = registrationModel.Email, Password = SecurityManager.GetHashString(registrationModel.Password), RegistrationDate = DateTime.Now.Date, Status = (short)UserStatus.Unconfirmed.Id }; try { Data.Users.Add(user); Data.SaveChanges(); if (imageUpload != null) { if (imageUpload.ContentLength <= 0 || !SecurityManager.IsImage(imageUpload)) { return ProcessResults.InvalidImageFormat; } user.Image = SaveImage(user.Id, StaticSettings.AvatarsUploadFolderPath, imageUpload, server); Data.SaveChanges(); } SendConfirmationMail(context, user,culture); } catch(Exception) { Data.Users.Remove(user); return ProcessResults.RegistrationError; } return ProcessResults.UserRegistered; }
public ProcessResult LogInUser(string email, string password, out User user) { user = Find(email); if (user == null) return ProcessResults.InvalidEmail; if (user.Password == SecurityManager.GetHashString(password)) { ProcessResult result = ProcessResults.LoginSuccessful; result.AffectedObjectId = user.Id; return result; } return ProcessResults.InvalidPassword; }
private bool HasNoAdminAccess(User user) { return user == null || !user.HasAdminAccess; }
private bool HasNoAccess(User user) { return user == null || !user.HasModeratorAccess; }