public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new Customer { UserName = model.Email, Email = model.Email }; var result = await SecureAuthUserManager.CreateAsync(user, model.Password); if (result.Succeeded) { string code = await SecureAuthUserManager.GenerateEmailConfirmationTokenAsync(user.Id); code = System.Web.HttpUtility.UrlEncode(code); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string mailBody = new MailTools().BodyGenerate(MailTools.EmailType.Account, callbackUrl); await SecureAuthUserManager.SendEmailAsync(user.Id, "確認您的帳戶", mailBody); return(RedirectToAction("Index", "Home")); } AddErrors(result); } return(View(model)); }
public async Task <ActionResult> ConfirmEmail(string userId, string code) { if (userId == null || code == null) { return(View("Error")); } code = System.Web.HttpUtility.UrlDecode(code); //外丟要再對應回主鍵 var result = await SecureAuthUserManager.ConfirmEmailAsync(int.Parse(userId), code); //if (result.Succeeded) //{ // return RedirectToAction("Index", "Home"); //} //else //{ // //轉向錯誤畫面 // return RedirectToAction("Index", "Home"); //} return(View()); }
public async Task <ActionResult> Login(LoginViewModel model) { //登出 SecureAuthUserSingInManager.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); // Create claims identity(移進去SecureAuthUserManager) //ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); // Add claims (使用者資料) //IList<Claim> claimList = new List<Claim>(); //identity.AddClaims(claimList); //連資料庫把客戶抓出來 var customer = CustomerRepository.GetById(model.Email); //處理登入cookie var identity = await SecureAuthUserManager.CreateIdentityAsync(customer, DefaultAuthenticationTypes.ApplicationCookie); SecureAuthUserSingInManager.AuthenticationManager.SignIn(new AuthenticationProperties { AllowRefresh = true, IsPersistent = false }, identity); return(RedirectToAction("Index")); }