public static int CreateUser(User newUser) { string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["PostgresDB"].ConnectionString; var conn = new NpgsqlConnection(connstring); conn.Open(); string sqlquery = "insert into users (username,pwd,useremail) values ('" + newUser.UserName + "','" + newUser.Password + "','" + newUser.Email + "')"; var cmd = new NpgsqlCommand(sqlquery, conn); int rowsaffected = cmd.ExecuteNonQuery(); conn.Close(); return rowsaffected; }
public string ActivateUserAccount([Bind(Include = "ActivationEmail,ActivationCode")] UserToActivateViewModel userToActivate) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); if (!ModelState.IsValid) throw new Exception("Walidacja użytkownika nie powiodła się"); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToActivate, user); var activationResult = user.Activate(); switch (activationResult) { case UserActionResult.Success: { return JsonConvert.SerializeObject(new { Message = $"Konto <span class=\"linklike\">{user.UserName}</span> zostało Aktywowane", Result = UserActionResult.Success, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.Success) }); } case UserActionResult.DatabaseError: { return JsonConvert.SerializeObject(new { Message = "Baza danych nie odpowiada", Result = UserActionResult.DatabaseError, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.DatabaseError) }); } case UserActionResult.AccountAlreadyActivated: { return JsonConvert.SerializeObject(new { Message = "Użytkownik został już aktywowany", Result = UserActionResult.AccountAlreadyActivated, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.AccountAlreadyActivated) }); } default: throw new ArgumentOutOfRangeException(); } }
public string SendRemindPasswordRequest([Bind(Include = "RemindPasswordEmail")] UserToSendRemindPasswordRequestViewModel userToSendRemindPasswordRequest) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); ModelState.Remove("ActivationCode"); if (!ModelState.IsValid) throw new Exception("Walidacja użytkownika nie powiodła się"); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToSendRemindPasswordRequest, user); var sendActivationResult = user.SendRemindPasswordRequest(); if (sendActivationResult == UserActionResult.DatabaseError) { return JsonConvert.SerializeObject(new { Message = "Baza danych nie odpowiada", Result = UserActionResult.DatabaseError, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.DatabaseError) }); } if (sendActivationResult == UserActionResult.SendingEmailFailure) { return JsonConvert.SerializeObject(new { Message = "Email z kodem weryfikacyjnym nie został wysłany", Result = UserActionResult.SendingEmailFailure, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.SendingEmailFailure) }); } if (sendActivationResult == UserActionResult.Success) { return JsonConvert.SerializeObject(new { Message = $"Kod weryfikacyjny wysłano do: <span class=\"linklike\">{user.RemindPasswordEmail}</span>", Result = UserActionResult.Success, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.Success) }); } throw new ArgumentOutOfRangeException(); }
public string RemindUserPassword([Bind(Include = "RemindPasswordEmail,RemindPasswordCode,RemindPasswordOldPassword,RemindPasswordNewPassword,RemindPasswordConfirmPassword")] UserToRemindPasswordViewModel userToRemindPassword) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); if (!ModelState.IsValid) throw new Exception("Walidacja użytkownika nie powiodła się"); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToRemindPassword, user); var activationResult = user.RemindPassword(); switch (activationResult) { case UserActionResult.Success: { return JsonConvert.SerializeObject(new { Message = $"Hasło do konta użytkownika: <span class=\"linklike\">{user.UserName}</span> zostało Zmienione", Result = UserActionResult.Success, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.Success) }); } case UserActionResult.DatabaseError: { return JsonConvert.SerializeObject(new { Message = "Baza danych nie odpowiada", Result = UserActionResult.DatabaseError, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.DatabaseError) }); } default: throw new ArgumentOutOfRangeException(); } }
public string RegisterUser([Bind(Include = "UserName,Password,ConfirmPassword,Email")] UserToRegisterViewModel userToRegister) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); if (!ModelState.IsValid) throw new Exception("Walidacja użytkownika nie powiodła się"); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToRegister, user); var registrationResult = user.Register(); var sendActivationResult = user.SendActivationLink(); if (registrationResult == UserActionResult.DatabaseError || sendActivationResult == UserActionResult.DatabaseError) { return JsonConvert.SerializeObject(new { Message = "Baza danych nie odpowiada", Result = UserActionResult.DatabaseError, ResultString = Enum.GetName(typeof (UserActionResult), UserActionResult.DatabaseError) }); } if (sendActivationResult == UserActionResult.AccountAlreadyActivated) { return JsonConvert.SerializeObject(new { Message = "Użytkownik został już aktywowany", Result = UserActionResult.AccountAlreadyActivated, ResultString = Enum.GetName(typeof(UserActionResult), UserActionResult.AccountAlreadyActivated) }); } if (sendActivationResult == UserActionResult.SendingEmailFailure) { return JsonConvert.SerializeObject(new { Message = "Rejestracja poprawna, ale Email aktywacyjny nie został wysłany", Result = UserActionResult.SendingEmailFailure, ResultString = Enum.GetName(typeof (UserActionResult), UserActionResult.SendingEmailFailure) }); } if (registrationResult == UserActionResult.Success && sendActivationResult == UserActionResult.Success) { return JsonConvert.SerializeObject(new { Message = $"Rejestracja prawidłowa, link aktywacyjny wysłano do: {user.Email}", Result = UserActionResult.Success, ResultString = Enum.GetName(typeof (UserActionResult), UserActionResult.Success) }); } throw new ArgumentOutOfRangeException(); }
public string IsEmailInDatabaseAjax(User user) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); return IsEmailInDatabase(user.Email ?? user.ActivationEmail ?? user.RemindPasswordEmail); }
// Panel Logowania public PartialViewResult GetLoginPanel(string controller, string action) { // Jeśli Użytkownik jest zalogowany i dane są poprawne var userCookie = Request.Cookies["LoggedUser"]; var userSession = (UserToLoginViewModel)Session["LoggedUser"]; if (userCookie != null) { var userToLogin = JsonConvert.DeserializeObject<UserToLoginViewModel>(userCookie.Value); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToLogin, user); if (user.Authenticate(true) == UserActionResult.Success) // (przy użyciu Hasha z cookie, a nie czystego hasła) return PartialView("_LoginPanelLogged", userToLogin); } else if (userSession != null) { var userToLogin = userSession; var user = new User(); AutoMapperConfiguration.Mapper.Map(userToLogin, user); if (user.Authenticate(true) == UserActionResult.Success) return PartialView("_LoginPanelLogged", userToLogin); } return PartialView("_LoginPanel", new UserToLoginViewModel()); }
public string LoginUser([Bind(Include = "UserName,Password,RememberMe")] UserToLoginViewModel userToLogin) { if (!Request.IsAjaxRequest()) throw new Exception("Zapytanie nie zostało wywołane jako zapytanie AJAX"); //Thread.Sleep(5000); var user = new User(); AutoMapperConfiguration.Mapper.Map(userToLogin, user); var isAuthenticated = user.Authenticate(); userToLogin.Id = user.Id; userToLogin.UserName = user.UserName; userToLogin.Password = user.Password; userToLogin.RememberMe = user.RememberMe; switch (isAuthenticated) { case UserActionResult.Success: { userToLogin.Id = user.Id; // Zapisz w Sesji Session["LoggedUser"] = userToLogin; // Zapisz w Cookies if (user.RememberMe) { Response.SetCookie(new HttpCookie("LoggedUser") { Value = JsonConvert.SerializeObject(userToLogin), Expires = DateTime.Now.AddDays(30) }); } return JsonConvert.SerializeObject(new { LoginMessage = "", PartialView = RenderPartialView("_LoginPanelLogged", userToLogin) }); } case UserActionResult.Failure: { return JsonConvert.SerializeObject(new { LoginMessage = $"Niepoprawne Dane. Prób: {4 - user.RetryAttempts}", PartialView = RenderPartialView("_LoginPanel", userToLogin) }); } case UserActionResult.UserDoesNotExist: { return JsonConvert.SerializeObject(new { LoginMessage = "Użytkownik nie istnieje", PartialView = RenderPartialView("_LoginPanel", userToLogin) }); } case UserActionResult.AccountNotActivated: { return JsonConvert.SerializeObject(new { LoginMessage = "Konto Nieaktywne", PartialView = RenderPartialView("_LoginPanel", userToLogin) }); } case UserActionResult.AccountLocked: { int? secondsToUnlock = null; if (user.LockedDateTime != null) secondsToUnlock = (int) (15 * 60 - DateTime.Now.Subtract((DateTime) user.LockedDateTime).TotalSeconds); if (secondsToUnlock < 0) secondsToUnlock = 0; var timeToUnlock = secondsToUnlock != null ? $"{secondsToUnlock / 60:00}" + ":" + $"{secondsToUnlock % 60:00}" // string.Format("{0:00}", secondsToUnlock % 60) : "błąd"; return JsonConvert.SerializeObject(new { LoginMessage = $"Zablokowano. Spróbuj za: {timeToUnlock}", PartialView = RenderPartialView("_LoginPanel", userToLogin) }); } case UserActionResult.DatabaseError: { return JsonConvert.SerializeObject(new { LoginMessage = "Baza Danych nie odpowiada", PartialView = RenderPartialView("_LoginPanel", userToLogin) }); } default: throw new ArgumentOutOfRangeException(); } }
public static User IsValid(string _username, string _pwd) { User user = null; string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["PostgresDB"].ConnectionString; var conn = new NpgsqlConnection(connstring); string sqlquery = "Select * From users Where username='******' And pwd='" + _pwd + "'"; var cmd = new NpgsqlCommand(sqlquery, conn); conn.Open(); NpgsqlDataReader reader = cmd.ExecuteReader(); user = new User(); if (reader.HasRows) { while (reader.Read()) { user.UserName = reader["username"].ToString(); user.Email = reader["useremail"].ToString(); } } else { user.UserName = null; } return user; }