public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new SMEQ.Models.ApplicationUser { UserName = model.Email, Email = model.Email }; user.ID_Customer = Guid.NewGuid().ToString(); user.IsMain = true; user.status = true; //await to create user completed var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { _Customer.CreatNewCustomerByNewUser(user.Id); _Setting.SetDefaultSetting(user.ID_Customer, model.Email); string roleName = "User"; // Check to see if Role Exists, if not create it if (!RoleManager.RoleExists(roleName)) { RoleManager.Create(new IdentityRole(roleName)); } UserManager.AddToRole(user.Id, roleName); return(RedirectToAction("AccountList", "Admin")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new SMEQ.Models.ApplicationUser { UserName = model.Email, Email = model.Email }; user.ID_Customer = Guid.NewGuid().ToString(); user.IsMain = true; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { _Customer.CreatNewCustomerByNewUser(user.Id); _Setting.SetDefaultSetting(user.ID_Customer, model.Email); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public async Task <ActionResult> RegisterAgent(RegisterViewModel model) { if (ModelState.IsValid) { var user = new SMEQ.Models.ApplicationUser { UserName = model.Email, Email = model.Email }; user.ID_Customer = _Customer.GetIDCustomerByUser(User.Identity.GetUserId()); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(View(model)); } } return(View(model)); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login")); } if (loginInfo.Email != null) { var user = await UserManager.FindByEmailAsync(loginInfo.Email); if (user != null && !loginInfo.Login.LoginProvider.Equals("Google")) { if (!await UserManager.IsEmailConfirmedAsync(user.Id)) { ViewBag.ID = user.Id; return(View("SendMailConfirm")); } } } // Sign in the user with this external login provider if the user already has a login var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false); switch (result) { case SignInStatus.Success: { return(RedirectToLocal(returnUrl)); } case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false })); case SignInStatus.Failure: default: if (loginInfo.Email == null) { // If the user does not have an account, then prompt the user to create an account ViewBag.ReturnUrl = returnUrl; ViewBag.LoginProvider = loginInfo.Login.LoginProvider; return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email })); } else { // If the user get sucess email, then prompt the user to create an account var user2 = new SMEQ.Models.ApplicationUser { UserName = loginInfo.Email, Email = loginInfo.Email }; var resultCreate = await UserManager.CreateAsync(user2); user2.ID_Customer = Guid.NewGuid().ToString(); user2.IsMain = true; if (resultCreate.Succeeded) { resultCreate = await UserManager.AddLoginAsync(user2.Id, loginInfo.Login); if (resultCreate.Succeeded) { _Customer.CreatNewCustomerByNewUser(user2.Id); _Setting.SetDefaultSetting(user2.ID_Customer, loginInfo.Email); await SignInManager.SignInAsync(user2, isPersistent : false, rememberBrowser : false); return(RedirectToLocal(returnUrl)); } } return(Content("Email của bạn đã được đăng ký")); } } }