Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Identity.UI.Services;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Localization;
  6. using DemoApp.Models;
  7. using DemoApp.ViewModels;
  8. using System;
  9. using System.Threading.Tasks;
  10.  
  11. namespace DemoApp.Controllers
  12. {
  13. public class AccountController : Controller
  14. {
  15. private readonly UserManager<User> _userManager;
  16. private readonly SignInManager<User> _signInManager;
  17. private readonly IEmailSender _emailSender;
  18. private readonly IStringLocalizer<AccountController> _localizer;
  19.  
  20. public AccountController(
  21. UserManager<User> userManager,
  22. SignInManager<User> signInManager,
  23. IEmailSender emailSender,
  24. IStringLocalizer<AccountController> localizer)
  25. {
  26. _userManager = userManager;
  27. _signInManager = signInManager;
  28. _emailSender = emailSender;
  29. _localizer = localizer;
  30. }
  31.  
  32. [HttpGet]
  33. [AllowAnonymous]
  34. public IActionResult Login()
  35. {
  36. if (!User.Identity.IsAuthenticated)
  37. {
  38. ViewBag.Title = _localizer["AccessPage"];
  39. return View();
  40. }
  41.  
  42. return RedirectToAction("Home", "Dashboard");
  43. }
  44.  
  45. [HttpPost]
  46. [AllowAnonymous]
  47. [ValidateAntiForgeryToken]
  48. public async Task<IActionResult> Login(LoginViewModel vm)
  49. {
  50. if (ModelState.IsValid)
  51. {
  52. var user = await _userManager.FindByEmailAsync(vm.Email);
  53.  
  54. if (user != null)
  55. {
  56. if (!user.EmailConfirmed)
  57. {
  58. ModelState.AddModelError(string.Empty, _localizer["EmailNotConfirmed"]);
  59. return View(vm);
  60. }
  61.  
  62. var result = await _signInManager
  63. .PasswordSignInAsync(user, vm.Password, isPersistent: vm.RememberMe, lockoutOnFailure: true);
  64.  
  65. await _userManager.UpdateSecurityStampAsync(user);
  66.  
  67. if (result.Succeeded)
  68. {
  69. return RedirectToAction("Home", "Dashboard");
  70. }
  71. else if (result.IsLockedOut)
  72. {
  73. return View("Lockout");
  74. }
  75. else
  76. {
  77. ModelState.AddModelError(string.Empty, _localizer["InvalidCredentials"]);
  78. }
  79. }
  80. }
  81.  
  82. return View(vm);
  83. }
  84.  
  85. [HttpGet]
  86. public IActionResult Register()
  87. {
  88. return View();
  89. }
  90.  
  91. [HttpPost]
  92. public async Task<IActionResult> Register(RegisterViewModel vm)
  93. {
  94. if (ModelState.IsValid)
  95. {
  96. if (await CheckUserExist(vm))
  97. return View(vm);
  98.  
  99. var user = new User
  100. {
  101. FirstName = vm.FirstName,
  102. LastName = vm.LastName,
  103. UserName = vm.UserName,
  104. Email = vm.Email
  105. };
  106.  
  107. var result = await _userManager.CreateAsync(user, vm.Password);
  108.  
  109. if (result.Succeeded)
  110. {
  111. await _userManager.AddToRoleAsync(user, "Customer");
  112.  
  113. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  114.  
  115. var callbackUrl = Url.Action("ConfirmEmail", "Account", new
  116. {
  117. userId = user.Id,
  118. token = code
  119. }, protocol: Request.Scheme);
  120.  
  121. await _emailSender.SendEmailAsync(user.Email, _localizer["ConfirmYourEmail"], _localizer["ConfirmAccountTemplate", callbackUrl]);
  122.  
  123. return View("RegisterConfirmation", user);
  124. }
  125. else
  126. {
  127. foreach (var error in result.Errors)
  128. {
  129. ModelState.AddModelError("", error.Description);
  130. }
  131. }
  132. }
  133.  
  134. return View(vm);
  135. }
  136.  
  137. [AllowAnonymous]
  138. [HttpGet]
  139. public async Task<IActionResult> ConfirmEmail(string userId, string token)
  140. {
  141. if (userId == null || token == null)
  142. {
  143. return RedirectToAction("Index", "Error");
  144. }
  145.  
  146. var user = await _userManager.FindByIdAsync(userId);
  147.  
  148. if (user != null)
  149. {
  150. if (user.EmailConfirmed)
  151. {
  152. ModelState.AddModelError(string.Empty, _localizer["EmailAlreadyConfirmed"]);
  153. return View(user);
  154. }
  155.  
  156. IdentityResult result;
  157.  
  158. try
  159. {
  160. result = await _userManager.ConfirmEmailAsync(user, token);
  161. }
  162. catch (InvalidOperationException ex)
  163. {
  164. return RedirectToAction("Index", "Error", new { errorMessage = ex.Message });
  165. }
  166.  
  167. if (result.Succeeded)
  168. {
  169. await _emailSender.SendEmailAsync(user.Email, _localizer["EmailConfirmed"], _localizer["EmailConfirmedTemplate"]);
  170. return View("ConfirmEmail", user);
  171. }
  172. }
  173.  
  174. ModelState.AddModelError(string.Empty, _localizer["UserNotFound"]);
  175. return View();
  176. }
  177.  
  178. private async Task<bool> CheckUserExist(RegisterViewModel vm)
  179. {
  180. var user = await _userManager.FindByNameAsync(vm.UserName);
  181.  
  182. if (user != null)
  183. {
  184. ModelState.AddModelError("username", _localizer["UsernameAlreadyTaken"]);
  185. return true;
  186. }
  187.  
  188. await _userManager.FindByEmailAsync(vm.Email);
  189.  
  190. if (user != null)
  191. {
  192. ModelState.AddModelError("email", _localizer["EmailAlreadyTaken"]);
  193. return true;
  194. }
  195.  
  196. return false;
  197. }
  198.  
  199. [HttpGet]
  200. public async Task<IActionResult> Logout()
  201. {
  202. if (User.Identity.IsAuthenticated)
  203. {
  204. await _signInManager.SignOutAsync();
  205. return RedirectToAction("Logout");
  206. }
  207. else
  208. {
  209. return View();
  210. }
  211. }
  212.  
  213. [HttpGet]
  214. [AllowAnonymous]
  215. public IActionResult ForgotPassword()
  216. {
  217. return View();
  218. }
  219.  
  220. [HttpPost]
  221. [AllowAnonymous]
  222. [ValidateAntiForgeryToken]
  223. public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel vm)
  224. {
  225. if (ModelState.IsValid)
  226. {
  227. var user = await _userManager.FindByEmailAsync(vm.Email);
  228.  
  229. if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
  230. {
  231. return View("ForgotPasswordConfirmation");
  232. }
  233.  
  234. var code = await _userManager.GeneratePasswordResetTokenAsync(user);
  235. var callbackUrl = Url.Action("ResetPassword", "Account", new
  236. {
  237. userEmail = user.Email,
  238. token = code
  239. }, protocol: Request.Scheme);
  240.  
  241. await _emailSender.SendEmailAsync(vm.Email, _localizer["PasswordReset"], _localizer["PasswordResetTemplate", callbackUrl]);
  242.  
  243. ViewBag.ResetPassword = true;
  244. return View("ForgotPasswordConfirmation");
  245. }
  246.  
  247. return View(vm);
  248. }
  249.  
  250. [HttpGet]
  251. [AllowAnonymous]
  252. public async Task<IActionResult> ResetPassword(string userEmail, string token)
  253. {
  254. if (userEmail == null || token == null)
  255. {
  256. return RedirectToAction("Index", "Error");
  257. }
  258.  
  259. var user = await _userManager.FindByEmailAsync(userEmail);
  260.  
  261. if (user != null)
  262. {
  263. return View(new ResetPasswordViewModel { Email = userEmail, Token = token });
  264. }
  265.  
  266. return RedirectToAction("Index", "Error");
  267. }
  268.  
  269. [HttpPost]
  270. [AllowAnonymous]
  271. public async Task<IActionResult> ResetPassword(ResetPasswordViewModel vm)
  272. {
  273. if (ModelState.IsValid)
  274. {
  275. var user = await _userManager.FindByEmailAsync(vm.Email);
  276.  
  277. if (user != null)
  278. {
  279. var result = await _userManager.ResetPasswordAsync(user, vm.Token, vm.Password);
  280.  
  281. if (result.Succeeded)
  282. {
  283. return View("ResetPasswordConfirmation");
  284. }
  285. else
  286. {
  287. ModelState.AddModelError(string.Empty, _localizer["ResetNotSuccess"]);
  288. }
  289. }
  290. }
  291.  
  292. return View(vm);
  293. }
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement