public async Task<ActionResult> Index() { if (!Request.IsAuthenticated) { var temporaryEmail = "temporary-user@" + Guid.NewGuid() + ".com"; var user = new ApplicationUser { UserName = temporaryEmail, Email = temporaryEmail, IsTemporary = true }; var result = await _userService.CreateTemporaryAsync(user); if (result.Succeeded) { await _authenticationService.SignInAsync(user, false, false); ViewData["IsTemporaryUser"] = true; } else { //handle cart storage. use session? } } else { string userId = _userService.GetUserId(); ViewData["IsTemporaryUser"] = _userService.IsTemporary(userId); } //loads the partial return View(); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { string oldUserId = _userService.GetUserId(); var wasAnonymous = _userService.IsTemporary(oldUserId); if (!wasAnonymous) { return new HttpStatusCodeResult(HttpStatusCode.Forbidden, "loggedIn"); } _authenticationService.SignOut(DefaultAuthenticationTypes.ApplicationCookie); var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userService.CreateAsync(user, model.Password); if (result.Succeeded) { await _authenticationService.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>"); try { if (!_cartService.HasEmptyCart(oldUserId)) { _cartService.Transfer(oldUserId, user.Id); } } catch (Exception) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } return new HttpStatusCodeResult(HttpStatusCode.OK); } AddErrors(result); } // If we got this far, something failed, redisplay form Response.StatusCode = (int)HttpStatusCode.BadRequest; return Json(model, JsonRequestBehavior.DenyGet); }
public async Task<ActionResult> LogOut() { _authenticationService.SignOut(DefaultAuthenticationTypes.ApplicationCookie); var temporaryEmail = "temporary-user@" + Guid.NewGuid() + ".com"; var user = new ApplicationUser { UserName = temporaryEmail, Email = temporaryEmail, IsTemporary = true }; //create a temporary user again var result = await _userService.CreateTemporaryAsync(user); if (result.Succeeded) { await _authenticationService.SignInAsync(user, false, false); ViewData["IsTemporaryUser"] = true; } return new HttpStatusCodeResult(HttpStatusCode.OK); }
public async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser) { await SignInManager.SignInAsync(user, isPersistent, rememberBrowser); }