コード例 #1
0
 public IActionResult NewUser(User user)
 {
     if (ModelState.IsValid)
     {
         if (dbContext.users.Any(u => u.UserName == user.UserName))
         {
             ModelState.AddModelError("UserName", "UserName already in use");
             return(View("Index"));
         }
         // Initializing a PasswordHasher object, providing our User class as its
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         user.Password = Hasher.HashPassword(user, user.Password);
         //Save your user object to the database
         dbContext.Add(user);
         dbContext.SaveChanges();
         HttpContext.Session.SetInt32("Id", user.UserId);
         HttpContext.Session.SetString("FirstName", user.FirstName);
         return(RedirectToAction("AllHobbies"));
     }
     else
     {
         return(View("Index"));
     }
 }
コード例 #2
0
 public IActionResult Register(User submitted_info)
 {
     submitted_info.CreatedAt = DateTime.Now;
     submitted_info.UpdatedAt = DateTime.Now;
     if (ModelState.IsValid)
     {
         // If a User exists with provided email
         if (dbContext.users.Any(u => u.UserName == submitted_info.UserName))
         {
             // Manually add a ModelState error to the Email field, with provided
             // error message
             ModelState.AddModelError("UserName", "UserName already in use!");
             return(View("RegisterView"));
             // You may consider returning to the View at this point
         }
         //LOGIC FOR ADDING TO DB
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         submitted_info.Password = Hasher.HashPassword(submitted_info, submitted_info.Password);
         HttpContext.Session.SetInt32("ID", submitted_info.UserId);
         dbContext.Add(submitted_info);
         dbContext.SaveChanges();
         return(RedirectToAction("loginuser"));
     }
     else
     {
         Console.WriteLine("NOT VALID**************", ModelState.Keys);
         foreach (var error in ModelState)
         {
             if (error.Value.Errors.Count > 0)
             {
                 Console.WriteLine("err msg " + error.Value.Errors[0].ErrorMessage);
             }
         }
         return(View("RegisterView"));
     }
 }