public async Task<ActionResult> Signup(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                User admin = new User();
                List<User> listAdmin = new List<User>();
                listAdmin = db.Users.ToList();
                var newAdmin = listAdmin.FindAll(m => m.UserName.Equals(model.Username));
                if (newAdmin.Count == 0)
                {
                    User newAdmin1 = new User();
                    newAdmin1.UserName = model.Username;
                    newAdmin1.UserPassword = model.Password;
                    db.Users.Add(newAdmin1);
                    db.SaveChanges();
                    ModelState.AddModelError("", "Sucessful!");
                }
                else
                {
                    ModelState.AddModelError("", "Username is existed");
                }
                //var user = new ApplicationUser { UserName = model.Username, Email = model.Email };
                //var result = await UserManager.CreateAsync(user, model.Password);
                //if (result.Succeeded)
                //{
                //    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                //    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                //    // Send an email with this link
                //    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //    return RedirectToAction("Index", "Home");
                //}
                //AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public async Task<ActionResult> Signin(AdminLoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                User admin = new User();
                List <User> listAdmin = new List<User>();
                listAdmin = db.Users.ToList();
                var newAdmin = listAdmin.FindAll(m => m.UserName.Equals(model.Username));
                
                if (newAdmin.Count == 0)
                {
                    
                    newAdmin = listAdmin.FindAll(m => m.UserPassword.Equals(model.Password));
                    if (newAdmin.Count == 0)
                    {
                        ModelState.AddModelError("", "Username and Password is incorrect!");
                       
                        return View(model);
                    }else
                    {
                        ModelState.AddModelError("", "Username is incorrect!");
                        return View(model);
                    }
                }
                else
                {
                    //newAdmin = listAdmin.FindAll(m => m.UserPassword.Equals(model.Password));
                    if (newAdmin[0].UserPassword.Trim().Equals(model.Password.Trim()))
                    {
                       
                         if (newAdmin[0].UserRoles == 2)
                        {
                            if (newAdmin[0].AccountStatus == false)
                            {
                                ModelState.AddModelError("", "Admin is blocked!");
                                return View(model);
                            }
                            else
                            {
                                Session["UserName"] = model.Username;

                                admin.UserName = model.Username;
                                admin.UserPassword = model.Password;

                                UserHelpers.SetCurrentUser(Session, admin);
                                return View("AdminView", admin);
                            }
                            
                        }
                        else
                        {
                            ModelState.AddModelError("", "Account is not an admin");
                            return View(model);
                        }
                        
                    }
                    else
                    {
                        ModelState.AddModelError("", "Password is incorrect!");
                       
                        return View(model);
                    }
                }

            }
            else
            {
                return RedirectToAction("AdminView", "Home");
            }
           
        }
예제 #3
0
 public static void SetCurrentUser(HttpSessionStateBase session, User user)
 {
     session[User] = user;
 }