//[ValidateAntiForgeryToken] public async Task <ActionResult> Register(RegisterPageWraper model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.RegisterViewModel.Email, Email = model.RegisterViewModel.Email }; var result = await UserManager.CreateAsync(user, model.RegisterViewModel.Password); if (result.Succeeded) { // mobile number var mobile = model.RegisterViewModel.Mobile; mobile = mobile.Substring(1).Replace("-", ""); model.RegisterViewModel.Mobile = "92" + mobile; UserServices.AddUserProperties(model.RegisterViewModel, user.Id); var uObj = UserServices.GetUserById(user.Id); SetUserSession(uObj, true); var content = "Hi " + model.RegisterViewModel.FullName + "!\nYou have been successfully registered as a " + model.RegisterViewModel.Type + " at KamSham.Pk."; if (model.RegisterViewModel.Type == "Contractor") { content = content + "You will be able to login once we approve your account information."; } content = content + "\n-KamSham Team\n+923084449991"; KaamShaam.Services.EmailService.SendEmail(user.Email, "Registration Notification | KamSham.Pk", content); TempData.Add("userId", user.Id); TempData.Add("userNumber", model.RegisterViewModel.Mobile); return(RedirectToAction("VerifyNumber", "Account")); //if (model.RegisterViewModel.Type == "Contractor") //{ // return RedirectToAction("Welcome", "Account"); //} // 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); } var cats = CategoryService.GetAllCategories(); model.Categories = cats; // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> AdminLogin(RegisterPageWraper model, string returnUrl) { if (!ModelState.IsValid) { return(View("AdminLogin")); } if (string.IsNullOrEmpty(returnUrl)) { returnUrl = "Admin/Stats"; } var findByEmail = UserManager.FindByEmail(model.LoginViewModel.Email); if (findByEmail == null) { ModelState.AddModelError("", "Invalid login attempt or admin does not exist"); return(GetLoginStuff()); } var uid = findByEmail.Id; var uObj = UserServices.GetUserById(uid); if (uObj.Roles.Any(r => r.ToLower().Contains("user") || r.ToLower().Contains("contractor"))) { ModelState.AddModelError("", "Invalid admin login attempt."); return(View("AdminLogin")); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.LoginViewModel.Email, model.LoginViewModel.Password, true, shouldLockout : false); switch (result) { case SignInStatus.Success: { SetUserSession(uObj); // return RedirectToLocal(returnUrl); return(RedirectToAction("Stats", "Admin")); } case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.LoginViewModel.RememberMe })); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid admin login attempt."); return(View("AdminLogin")); } }
public async Task <ActionResult> Login(RegisterPageWraper model, string returnUrl) { if (!ModelState.IsValid) { return(RedirectToAction("Login", "Account")); } if (string.IsNullOrEmpty(returnUrl)) { returnUrl = "Home/Index"; } var findByEmail = UserManager.FindByEmail(model.LoginViewModel.Email); if (findByEmail == null) { ModelState.AddModelError("", "Invalid login attempt or user does not exist"); return(GetLoginStuff()); } var uid = findByEmail.Id; var uObj = UserServices.GetUserById(uid); if (uObj.Roles.Any(r => r.ToLower().Contains("admin") || r.ToLower().Contains("super admin"))) { ModelState.AddModelError("", "Invalid login attempt."); return(GetLoginStuff()); } if (!uObj.PhoneNumberConfirmed) { TempData.Add("userId", uObj.Id); TempData.Add("userNumber", uObj.Mobile); return(RedirectToAction("VerifyNumber", "Account")); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.LoginViewModel.Email, model.LoginViewModel.Password, true, shouldLockout : false); switch (result) { case SignInStatus.Success: { var isApproved = UserAdminService.IsUserApproved(model.LoginViewModel.Email); if (!isApproved) { var mesge = string.IsNullOrEmpty(uObj.Feedback) ? "User is not approved by admin." : uObj.Feedback; ModelState.AddModelError("", mesge); return(GetLoginStuff()); } SetUserSession(uObj); returnUrl = uObj.Type == "User" ? "/Job/ManageJobs" : "/Job/findJobs"; return(RedirectToLocal(returnUrl)); } case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.LoginViewModel.RememberMe })); case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return(GetLoginStuff()); } }