public static bool Create(RegisterModel data) { if (_userMethods.UserExists(data.Username)) { return(false); } if (_userMethods.EmailExists(data.Email)) { return(false); } var newUser = new User { Username = data.Username, DisplayName = data.Username, Email = data.Email, Salt = UserValidation.GenerateSalt(), HasGravatar = false, ProfilePic = "" }; newUser.Password = UserValidation.HashText(data.Password, newUser.Salt, new SHA512CryptoServiceProvider()); _userMethods.CreateUser(newUser); if (!_userMethods.UserExists(newUser.Username)) { return(false); } var user = _userMethods.GetUserByUsername(newUser.Username); var verification = UserValidation.GenerateSalt(); _userMethods.AddVerification(user.Id, verification); Email.Send("Registration", data.Email, "Verification", new Dictionary <string, string> { { "username", data.Username }, { "verification", verification }, { "url", Settings.Url } }, _emailTemplateMethods); return(true); }
public static (bool password, bool verified) Login(string usernameEmail, string password) { User user = null; var verified = true; var vPassword = true; if (_userMethods.UserExists(usernameEmail)) { user = (User)_userMethods.GetUserByUsername(usernameEmail); } else if (_userMethods.EmailExists(usernameEmail)) { user = (User)_userMethods.GetUserByEmail(usernameEmail); } else { vPassword = false; } if (!vPassword) { return(vPassword, verified); } if (!user.Verified) { verified = false; } vPassword = user.Password == UserValidation.HashText(password, user.Salt, new SHA512CryptoServiceProvider()); if (vPassword && verified) { var manager = new SessionIDManager(); bool redirected; bool isAdded; var oldID = UserSession.CurrentContext.Session.SessionID; var id = manager.CreateSessionID(UserSession.CurrentContext); UserSession.CurrentUser = user; var oldDate = user.LastLogin; user.LastLogin = DateTime.UtcNow; _userMethods.UpdateUser(user); user.LastLogin = oldDate; UserSession.AddTempSession(id, UserSession.CurrentContext.Session); manager.RemoveSessionID(UserSession.CurrentContext); manager.SaveSessionID(UserSession.CurrentContext, id, out redirected, out isAdded); for (var i = 0; i < UserSession.CurrentContext.Response.Cookies.Count; i++) { var cookie = UserSession.CurrentContext.Response.Cookies.Get(i); if (cookie != null && cookie.Value == id) { var current = cookie; current.Expires = DateTime.Now.AddMonths(2); UserSession.CurrentContext.Response.Cookies.Remove(current.Name); UserSession.CurrentContext.Response.Cookies.Add(current); } } } return(vPassword, verified); }
private static string GenereateGravatarByEmail(string email, bool hasGravatar = false, string gravatarType = "retro") { var hash = UserValidation.HashText(email, "", new MD5CryptoServiceProvider()); return("https://www.gravatar.com/avatar/" + hash + (!hasGravatar ? "?d=" + gravatarType + "&s=100" : "?s=100")); }