Guest User

token reset

a guest
May 18th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1.         //
  2.         // GET: /Account/Upgrade
  3.         [HttpGet]
  4.         [AllowAnonymous]
  5.         public IActionResult Upgrade(string returnUrl = null)
  6.         {
  7.             if (User.Identity.IsAuthenticated)
  8.             {
  9.                 return RedirectToAction("Index", "Home");
  10.             }
  11.             ViewData["ReturnUrl"] = returnUrl;
  12.             return View();
  13.         }
  14.  
  15.         //
  16.         // POST: /Account/Upgrade
  17.         [HttpPost]
  18.         [AllowAnonymous]
  19.         [ValidateAntiForgeryToken]
  20.         public async Task<IActionResult> Upgrade(UpgradeViewModel model, string returnUrl = null)
  21.         {
  22.             ViewData["ReturnUrl"] = returnUrl;
  23.             if (ModelState.IsValid)
  24.             {
  25.                 if (User.Identity.IsAuthenticated)
  26.                 {
  27.                     return RedirectToAction("Index", "Home");
  28.                 }
  29.                 var user = await _userManager.FindByNameAsync(model.UserName);
  30.                 var password = CreateMD5("2011" + model.Password).ToLower();
  31.  
  32.                 if (user != null && user.PasswordHash == password)
  33.                 {
  34.                     var code = await _userManager.GeneratePasswordResetTokenAsync(user);
  35.                     return RedirectToAction(nameof(Finalize), new { code, user.UserName });
  36.                 }
  37.                 else
  38.                 {
  39.                     ModelState.AddModelError(string.Empty, "Identifiant ou mot de passe incorrect.");
  40.                 }
  41.                 return View(model);
  42.             }
  43.  
  44.             // If we got this far, something failed, redisplay form
  45.             return View(model);
  46.         }
  47.  
  48.         //
  49.         // GET: /Account/Upgrade
  50.         [HttpGet]
  51.         [AllowAnonymous]
  52.         public IActionResult Finalize(string code, string UserName)
  53.         {
  54.             if (User.Identity.IsAuthenticated)
  55.             {
  56.                 return RedirectToAction("Index", "Home");
  57.             }
  58.             var model = new FinalizeViewModel();
  59.             model.UserName = UserName;
  60.             model.code = code;
  61.             return View(model);
  62.         }
  63.  
  64.         //
  65.         // POST: /Account/Upgrade
  66.         [HttpPost]
  67.         [AllowAnonymous]
  68.         [ValidateAntiForgeryToken]
  69.         public async Task<IActionResult> Finalize(FinalizeViewModel model, string returnUrl = null)
  70.         {
  71.             ViewData["ReturnUrl"] = returnUrl;
  72.             if (ModelState.IsValid)
  73.             {
  74.                 if (User.Identity.IsAuthenticated)
  75.                 {
  76.                     return RedirectToAction("Index", "Home");
  77.                 }
  78.                 var user = await _userManager.FindByNameAsync(model.UserName);
  79.                 if (user == null)
  80.                 {
  81.                     // Don't reveal that the user does not exist
  82.                     return RedirectToAction(nameof(AccountController.Upgrade), "Account");
  83.                 }
  84.                 await _userManager.UpdateSecurityStampAsync(user);
  85.                 var result = await _userManager.ResetPasswordAsync(user, Model.code, model.Password);
  86.                 if (result.Succeeded)
  87.                 {
  88.                     return RedirectToAction(nameof(AccountController.Login), "Account");
  89.                 }
  90.                 AddErrors(result);
  91.                 return View(model);
  92.             }
  93.  
  94.             // If we got this far, something failed, redisplay form
  95.             return View(model);
  96.         }
Add Comment
Please, Sign In to add comment