Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.Encodings.Web;
  6. using System.Threading.Tasks;
  7. using Microsoft.AspNetCore.Authentication;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.Identity;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using WelfareDenmarkMVC.Models;
  14. using WelfareDenmarkMVC.Models.ManageViewModels;
  15. using WelfareDenmarkMVC.Services;
  16. using Microsoft.EntityFrameworkCore;
  17. using WelfareDenmarkMVC.Data;
  18. using WelfareDenmarkMVC.Models.AccountViewModels;
  19. using System.IO;
  20. using Microsoft.AspNetCore.Hosting;
  21. using Microsoft.AspNetCore.Http;
  22. using System.Security.Claims;
  23.  
  24. namespace WelfareDenmarkMVC.Controllers
  25. {
  26. [Authorize]
  27. [Route("[controller]/[action]")]
  28. public class ManageController : Controller
  29. {
  30. private readonly UserManager<ApplicationUser> _userManager;
  31. private readonly SignInManager<ApplicationUser> _signInManager;
  32. private readonly IEmailSender _emailSender;
  33. private readonly ILogger _logger;
  34. private readonly UrlEncoder _urlEncoder;
  35. private readonly ApplicationDbContext _context;
  36. private readonly GalleryImageViewModel _galleryManager;
  37.  
  38. private IHostingEnvironment _env;
  39.  
  40.  
  41. private const string AuthenticatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6";
  42. private const string RecoveryCodesKey = nameof(RecoveryCodesKey);
  43.  
  44. public ManageController(
  45. UserManager<ApplicationUser> userManager,
  46. SignInManager<ApplicationUser> signInManager,
  47. GalleryImageViewModel galleryImageViewModel,
  48. IEmailSender emailSender,
  49. ILogger<ManageController> logger,
  50. UrlEncoder urlEncoder,
  51. ApplicationDbContext context,
  52. IHostingEnvironment env)
  53. {
  54. _userManager = userManager;
  55. _signInManager = signInManager;
  56. _emailSender = emailSender;
  57. _logger = logger;
  58. _urlEncoder = urlEncoder;
  59. _context = context;
  60. _env = env;
  61. _galleryManager = galleryImageViewModel;
  62. }
  63.  
  64. [TempData]
  65. public string StatusMessage { get; set; }
  66.  
  67. [HttpGet]
  68. public async Task<IActionResult> Index()
  69. {
  70. var user = await _userManager.GetUserAsync(User);
  71. if (user == null)
  72. {
  73. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  74. }
  75.  
  76. var model = new IndexViewModel
  77. {
  78. Username = user.UserName,
  79. Email = user.Email,
  80. PhoneNumber = user.PhoneNumber,
  81. IsEmailConfirmed = user.EmailConfirmed,
  82. StatusMessage = StatusMessage,
  83. Address = user.Address
  84. };
  85.  
  86. return View(model);
  87. }
  88.  
  89. [HttpPost]
  90. [ValidateAntiForgeryToken]
  91. public async Task<IActionResult> Index(IndexViewModel model)
  92. {
  93. if (!ModelState.IsValid)
  94. {
  95. return View(model);
  96. }
  97.  
  98. var user = await _userManager.GetUserAsync(User);
  99. user.Address = model.Address;
  100.  
  101. if (user == null)
  102. {
  103. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  104. }
  105.  
  106. var email = user.Email;
  107. if (model.Email != email)
  108. {
  109. var setEmailResult = await _userManager.SetEmailAsync(user, model.Email);
  110. if (!setEmailResult.Succeeded)
  111. {
  112. throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
  113. }
  114. }
  115.  
  116. var phoneNumber = user.PhoneNumber;
  117. if (model.PhoneNumber != phoneNumber)
  118. {
  119. var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, model.PhoneNumber);
  120. if (!setPhoneResult.Succeeded)
  121. {
  122. throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
  123. }
  124. }
  125.  
  126. await _userManager.UpdateAsync(user);
  127.  
  128. StatusMessage = "Din profil blev opdateret";
  129. return RedirectToAction(nameof(Index));
  130. }
  131.  
  132. [HttpPost]
  133. [ValidateAntiForgeryToken]
  134. public async Task<IActionResult> SendVerificationEmail(IndexViewModel model)
  135. {
  136. if (!ModelState.IsValid)
  137. {
  138. return View(model);
  139. }
  140.  
  141. var user = await _userManager.GetUserAsync(User);
  142. if (user == null)
  143. {
  144. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  145. }
  146.  
  147. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  148. var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
  149. var email = user.Email;
  150. await _emailSender.SendEmailConfirmationAsync(email, callbackUrl);
  151.  
  152. StatusMessage = "Emailbekræftelse sendt. Tjek din mail!";
  153. return RedirectToAction(nameof(Index));
  154. }
  155.  
  156. [HttpGet]
  157. public async Task<IActionResult> ChangePassword()
  158. {
  159. var user = await _userManager.GetUserAsync(User);
  160. if (user == null)
  161. {
  162. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  163. }
  164.  
  165. var hasPassword = await _userManager.HasPasswordAsync(user);
  166. if (!hasPassword)
  167. {
  168. return RedirectToAction(nameof(SetPassword));
  169. }
  170.  
  171. var model = new ChangePasswordViewModel { StatusMessage = StatusMessage };
  172. return View(model);
  173. }
  174.  
  175.  
  176. [HttpGet]
  177. public IActionResult Gallery()
  178. {
  179. return View();
  180. }
  181. [HttpPost]
  182. public async Task<IActionResult> Gallery(IFormFile imageToBeUploaded)
  183. {
  184. ClaimsPrincipal currentUser = User;
  185. var currentUserId = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
  186. ApplicationUser user = await _userManager.FindByIdAsync(currentUserId);
  187. _galleryManager.ApplicationUser = user;
  188.  
  189. if (!ModelState.IsValid)
  190. {
  191. return View(_galleryManager);
  192. }
  193.  
  194. if (imageToBeUploaded != null)
  195. {
  196. using (var memoryStream = new MemoryStream())
  197. {
  198. await imageToBeUploaded.CopyToAsync(memoryStream);
  199. var imageToBeUploadedByteArray = memoryStream.ToArray();
  200. _galleryManager.Image = imageToBeUploadedByteArray;
  201. }
  202. }
  203.  
  204. _context.GalleryImage.Add(_galleryManager);
  205. await _context.SaveChangesAsync();
  206. return View();
  207. }
  208. [HttpGet]
  209. public IActionResult Medicine()
  210. {
  211. return View();
  212. }
  213.  
  214. [HttpGet]
  215. public IActionResult Calendar()
  216. {
  217. return View();
  218. }
  219.  
  220. [HttpGet]
  221. public async Task<IActionResult> ContactPersons()
  222. {
  223. return View(await _context.Contacts.ToListAsync());
  224. }
  225.  
  226.  
  227.  
  228. [HttpPost]
  229. [ValidateAntiForgeryToken]
  230. public async Task<IActionResult> ChangePassword(ChangePasswordViewModel model)
  231. {
  232. if (!ModelState.IsValid)
  233. {
  234. return View(model);
  235. }
  236.  
  237. var user = await _userManager.GetUserAsync(User);
  238. if (user == null)
  239. {
  240. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  241. }
  242.  
  243. var changePasswordResult = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);
  244. if (!changePasswordResult.Succeeded)
  245. {
  246. AddErrors(changePasswordResult);
  247. return View(model);
  248. }
  249.  
  250. await _signInManager.SignInAsync(user, isPersistent: false);
  251. _logger.LogInformation("User changed their password successfully.");
  252. StatusMessage = "Your password has been changed.";
  253.  
  254. return RedirectToAction(nameof(ChangePassword));
  255. }
  256.  
  257. [HttpGet]
  258. public async Task<IActionResult> SetPassword()
  259. {
  260. var user = await _userManager.GetUserAsync(User);
  261. if (user == null)
  262. {
  263. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  264. }
  265.  
  266. var hasPassword = await _userManager.HasPasswordAsync(user);
  267.  
  268. if (hasPassword)
  269. {
  270. return RedirectToAction(nameof(ChangePassword));
  271. }
  272.  
  273. var model = new SetPasswordViewModel { StatusMessage = StatusMessage };
  274. return View(model);
  275. }
  276.  
  277. [HttpPost]
  278. [ValidateAntiForgeryToken]
  279. public async Task<IActionResult> SetPassword(SetPasswordViewModel model)
  280. {
  281. if (!ModelState.IsValid)
  282. {
  283. return View(model);
  284. }
  285.  
  286. var user = await _userManager.GetUserAsync(User);
  287. if (user == null)
  288. {
  289. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  290. }
  291.  
  292. var addPasswordResult = await _userManager.AddPasswordAsync(user, model.NewPassword);
  293. if (!addPasswordResult.Succeeded)
  294. {
  295. AddErrors(addPasswordResult);
  296. return View(model);
  297. }
  298.  
  299. await _signInManager.SignInAsync(user, isPersistent: false);
  300. StatusMessage = "Your password has been set.";
  301.  
  302. return RedirectToAction(nameof(SetPassword));
  303. }
  304.  
  305. [HttpGet]
  306. public async Task<IActionResult> ExternalLogins()
  307. {
  308. var user = await _userManager.GetUserAsync(User);
  309. if (user == null)
  310. {
  311. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  312. }
  313.  
  314. var model = new ExternalLoginsViewModel { CurrentLogins = await _userManager.GetLoginsAsync(user) };
  315. model.OtherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
  316. .Where(auth => model.CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
  317. .ToList();
  318. model.ShowRemoveButton = await _userManager.HasPasswordAsync(user) || model.CurrentLogins.Count > 1;
  319. model.StatusMessage = StatusMessage;
  320.  
  321. return View(model);
  322. }
  323.  
  324. [HttpPost]
  325. [ValidateAntiForgeryToken]
  326. public async Task<IActionResult> LinkLogin(string provider)
  327. {
  328. // Clear the existing external cookie to ensure a clean login process
  329. await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
  330.  
  331. // Request a redirect to the external login provider to link a login for the current user
  332. var redirectUrl = Url.Action(nameof(LinkLoginCallback));
  333. var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User));
  334. return new ChallengeResult(provider, properties);
  335. }
  336.  
  337. [HttpGet]
  338. public async Task<IActionResult> LinkLoginCallback()
  339. {
  340. var user = await _userManager.GetUserAsync(User);
  341. if (user == null)
  342. {
  343. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  344. }
  345.  
  346. var info = await _signInManager.GetExternalLoginInfoAsync(user.Id);
  347. if (info == null)
  348. {
  349. throw new ApplicationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'.");
  350. }
  351.  
  352. var result = await _userManager.AddLoginAsync(user, info);
  353. if (!result.Succeeded)
  354. {
  355. throw new ApplicationException($"Unexpected error occurred adding external login for user with ID '{user.Id}'.");
  356. }
  357.  
  358. // Clear the existing external cookie to ensure a clean login process
  359. await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
  360.  
  361. StatusMessage = "The external login was added.";
  362. return RedirectToAction(nameof(ExternalLogins));
  363. }
  364.  
  365. [HttpPost]
  366. [ValidateAntiForgeryToken]
  367. public async Task<IActionResult> RemoveLogin(RemoveLoginViewModel model)
  368. {
  369. var user = await _userManager.GetUserAsync(User);
  370. if (user == null)
  371. {
  372. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  373. }
  374.  
  375. var result = await _userManager.RemoveLoginAsync(user, model.LoginProvider, model.ProviderKey);
  376. if (!result.Succeeded)
  377. {
  378. throw new ApplicationException($"Unexpected error occurred removing external login for user with ID '{user.Id}'.");
  379. }
  380.  
  381. await _signInManager.SignInAsync(user, isPersistent: false);
  382. StatusMessage = "The external login was removed.";
  383. return RedirectToAction(nameof(ExternalLogins));
  384. }
  385.  
  386. [HttpGet]
  387. public async Task<IActionResult> TwoFactorAuthentication()
  388. {
  389. var user = await _userManager.GetUserAsync(User);
  390. if (user == null)
  391. {
  392. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  393. }
  394.  
  395. var model = new TwoFactorAuthenticationViewModel
  396. {
  397. HasAuthenticator = await _userManager.GetAuthenticatorKeyAsync(user) != null,
  398. Is2faEnabled = user.TwoFactorEnabled,
  399. RecoveryCodesLeft = await _userManager.CountRecoveryCodesAsync(user),
  400. };
  401.  
  402. return View(model);
  403. }
  404.  
  405. [HttpGet]
  406. public async Task<IActionResult> Disable2faWarning()
  407. {
  408. var user = await _userManager.GetUserAsync(User);
  409. if (user == null)
  410. {
  411. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  412. }
  413.  
  414. if (!user.TwoFactorEnabled)
  415. {
  416. throw new ApplicationException($"Unexpected error occured disabling 2FA for user with ID '{user.Id}'.");
  417. }
  418.  
  419. return View(nameof(Disable2fa));
  420. }
  421.  
  422. [HttpPost]
  423. [ValidateAntiForgeryToken]
  424. public async Task<IActionResult> Disable2fa()
  425. {
  426. var user = await _userManager.GetUserAsync(User);
  427. if (user == null)
  428. {
  429. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  430. }
  431.  
  432. var disable2faResult = await _userManager.SetTwoFactorEnabledAsync(user, false);
  433. if (!disable2faResult.Succeeded)
  434. {
  435. throw new ApplicationException($"Unexpected error occured disabling 2FA for user with ID '{user.Id}'.");
  436. }
  437.  
  438. _logger.LogInformation("User with ID {UserId} has disabled 2fa.", user.Id);
  439. return RedirectToAction(nameof(TwoFactorAuthentication));
  440. }
  441.  
  442. [HttpGet]
  443. public async Task<IActionResult> EnableAuthenticator()
  444. {
  445. var user = await _userManager.GetUserAsync(User);
  446. if (user == null)
  447. {
  448. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  449. }
  450.  
  451. var model = new EnableAuthenticatorViewModel();
  452. await LoadSharedKeyAndQrCodeUriAsync(user, model);
  453.  
  454. return View(model);
  455. }
  456.  
  457. [HttpPost]
  458. [ValidateAntiForgeryToken]
  459. public async Task<IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel model)
  460. {
  461. var user = await _userManager.GetUserAsync(User);
  462. if (user == null)
  463. {
  464. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  465. }
  466.  
  467. if (!ModelState.IsValid)
  468. {
  469. await LoadSharedKeyAndQrCodeUriAsync(user, model);
  470. return View(model);
  471. }
  472.  
  473. // Strip spaces and hypens
  474. var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty);
  475.  
  476. var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync(
  477. user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode);
  478.  
  479. if (!is2faTokenValid)
  480. {
  481. ModelState.AddModelError("Code", "Verification code is invalid.");
  482. await LoadSharedKeyAndQrCodeUriAsync(user, model);
  483. return View(model);
  484. }
  485.  
  486. await _userManager.SetTwoFactorEnabledAsync(user, true);
  487. _logger.LogInformation("User with ID {UserId} has enabled 2FA with an authenticator app.", user.Id);
  488. var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
  489. TempData[RecoveryCodesKey] = recoveryCodes.ToArray();
  490.  
  491. return RedirectToAction(nameof(ShowRecoveryCodes));
  492. }
  493.  
  494. [HttpGet]
  495. public IActionResult ShowRecoveryCodes()
  496. {
  497. var recoveryCodes = (string[])TempData[RecoveryCodesKey];
  498. if (recoveryCodes == null)
  499. {
  500. return RedirectToAction(nameof(TwoFactorAuthentication));
  501. }
  502.  
  503. var model = new ShowRecoveryCodesViewModel { RecoveryCodes = recoveryCodes };
  504. return View(model);
  505. }
  506.  
  507. [HttpGet]
  508. public IActionResult ResetAuthenticatorWarning()
  509. {
  510. return View(nameof(ResetAuthenticator));
  511. }
  512.  
  513. [HttpPost]
  514. [ValidateAntiForgeryToken]
  515. public async Task<IActionResult> ResetAuthenticator()
  516. {
  517. var user = await _userManager.GetUserAsync(User);
  518. if (user == null)
  519. {
  520. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  521. }
  522.  
  523. await _userManager.SetTwoFactorEnabledAsync(user, false);
  524. await _userManager.ResetAuthenticatorKeyAsync(user);
  525. _logger.LogInformation("User with id '{UserId}' has reset their authentication app key.", user.Id);
  526.  
  527. return RedirectToAction(nameof(EnableAuthenticator));
  528. }
  529.  
  530. [HttpGet]
  531. public async Task<IActionResult> GenerateRecoveryCodesWarning()
  532. {
  533. var user = await _userManager.GetUserAsync(User);
  534. if (user == null)
  535. {
  536. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  537. }
  538.  
  539. if (!user.TwoFactorEnabled)
  540. {
  541. throw new ApplicationException($"Cannot generate recovery codes for user with ID '{user.Id}' because they do not have 2FA enabled.");
  542. }
  543.  
  544. return View(nameof(GenerateRecoveryCodes));
  545. }
  546.  
  547. [HttpPost]
  548. [ValidateAntiForgeryToken]
  549. public async Task<IActionResult> GenerateRecoveryCodes()
  550. {
  551. var user = await _userManager.GetUserAsync(User);
  552. if (user == null)
  553. {
  554. throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  555. }
  556.  
  557. if (!user.TwoFactorEnabled)
  558. {
  559. throw new ApplicationException($"Cannot generate recovery codes for user with ID '{user.Id}' as they do not have 2FA enabled.");
  560. }
  561.  
  562. var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
  563. _logger.LogInformation("User with ID {UserId} has generated new 2FA recovery codes.", user.Id);
  564.  
  565. var model = new ShowRecoveryCodesViewModel { RecoveryCodes = recoveryCodes.ToArray() };
  566.  
  567. return View(nameof(ShowRecoveryCodes), model);
  568. }
  569.  
  570. #region Helpers
  571.  
  572. private void AddErrors(IdentityResult result)
  573. {
  574. foreach (var error in result.Errors)
  575. {
  576. ModelState.AddModelError(string.Empty, error.Description);
  577. }
  578. }
  579.  
  580. private string FormatKey(string unformattedKey)
  581. {
  582. var result = new StringBuilder();
  583. int currentPosition = 0;
  584. while (currentPosition + 4 < unformattedKey.Length)
  585. {
  586. result.Append(unformattedKey.Substring(currentPosition, 4)).Append(" ");
  587. currentPosition += 4;
  588. }
  589. if (currentPosition < unformattedKey.Length)
  590. {
  591. result.Append(unformattedKey.Substring(currentPosition));
  592. }
  593.  
  594. return result.ToString().ToLowerInvariant();
  595. }
  596.  
  597. private string GenerateQrCodeUri(string email, string unformattedKey)
  598. {
  599. return string.Format(
  600. AuthenticatorUriFormat,
  601. _urlEncoder.Encode("WelfareDenmarkMVC"),
  602. _urlEncoder.Encode(email),
  603. unformattedKey);
  604. }
  605.  
  606. private async Task LoadSharedKeyAndQrCodeUriAsync(ApplicationUser user, EnableAuthenticatorViewModel model)
  607. {
  608. var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
  609. if (string.IsNullOrEmpty(unformattedKey))
  610. {
  611. await _userManager.ResetAuthenticatorKeyAsync(user);
  612. unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
  613. }
  614.  
  615. model.SharedKey = FormatKey(unformattedKey);
  616. model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
  617. }
  618.  
  619. #endregion
  620. }
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement