|
| 1 | +// Identity Example from Microsoft Github |
| 2 | +//https://github.com/aspnet/Identity/blob/dev/samples/IdentitySample.Mvc/Controllers/AccountController.cs |
| 3 | +//TODO *Implement External Login (fb,google,github) *Email verification |
| 4 | + |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Authorization; |
| 7 | +using Microsoft.AspNetCore.Identity; |
| 8 | +using Microsoft.AspNetCore.Mvc; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using BareMetalApi.Models; |
| 11 | + |
| 12 | + |
| 13 | +namespace BareMetalApi.Controllers |
| 14 | +{ |
| 15 | + [Route("blog/[controller]")] |
| 16 | + public class AccountController : ControllerBase |
| 17 | + { |
| 18 | + private readonly UserManager<ApplicationUser> _userManager; |
| 19 | + private readonly SignInManager<ApplicationUser> _signInManager; |
| 20 | + private readonly ILogger _logger; |
| 21 | + |
| 22 | + public AccountController( |
| 23 | + UserManager<ApplicationUser> userManager, |
| 24 | + SignInManager<ApplicationUser> signInManager, |
| 25 | + ILoggerFactory loggerFactory) |
| 26 | + { |
| 27 | + _userManager = userManager; |
| 28 | + _signInManager = signInManager; |
| 29 | + _logger = loggerFactory.CreateLogger<AccountController>(); |
| 30 | + } |
| 31 | + |
| 32 | + // POST: /Account/login |
| 33 | + [HttpPost("login")] |
| 34 | + [AllowAnonymous] |
| 35 | + public async Task<dynamic> Login([FromBody] ApplicationUser model) |
| 36 | + { |
| 37 | + |
| 38 | + var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; |
| 39 | + var result = await _signInManager.PasswordSignInAsync(model.Email, model.PasswordHash, isPersistent: true, lockoutOnFailure: false); |
| 40 | + if (result.Succeeded) |
| 41 | + { |
| 42 | + |
| 43 | + // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 |
| 44 | + // Send an email with this link |
| 45 | + // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); |
| 46 | + // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); |
| 47 | + // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); |
| 48 | + |
| 49 | + return Ok(); |
| 50 | + } |
| 51 | + return BadRequest(); |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + // This doesn't count login failures towards account lockout |
| 57 | + // To enable password failures to trigger account lockout, set lockoutOnFailure: true |
| 58 | + //if (req.Email == null || req.Password == null) return new { authenticated = false }; |
| 59 | + //var result = await _signInManager.PasswordSignInAsync(req.Email, req.Password, isPersistent: true, lockoutOnFailure: false); |
| 60 | + //if (result.Succeeded) |
| 61 | + //{ |
| 62 | + //DateTime? expires = DateTime.UtcNow.AddMinutes(2); |
| 63 | + //var token = GetToken(req.Email, expires); |
| 64 | + //return new { authenticated = true, entityId = 1, token = token, tokenExpires = expires }; |
| 65 | + //} |
| 66 | + |
| 67 | + //return new { authenticated = false }; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + // POST: /Account/register |
| 73 | + [HttpPost("register")] |
| 74 | + [AllowAnonymous] |
| 75 | + public async Task<dynamic> Register([FromBody] ApplicationUser model) |
| 76 | + { |
| 77 | + var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; |
| 78 | + var result = await _userManager.CreateAsync(user, model.PasswordHash); |
| 79 | + if (result.Succeeded) |
| 80 | + { |
| 81 | + await _signInManager.SignInAsync(user, false); |
| 82 | + |
| 83 | + // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 |
| 84 | + // Send an email with this link |
| 85 | + // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); |
| 86 | + // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); |
| 87 | + // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); |
| 88 | + |
| 89 | + return Ok(); |
| 90 | + } |
| 91 | + return BadRequest(); |
| 92 | + } |
| 93 | + |
| 94 | + // |
| 95 | + // POST: /Account/LogOff |
| 96 | + [HttpPost] |
| 97 | + public async Task<IActionResult> LogOff() |
| 98 | + { |
| 99 | + await _signInManager.SignOutAsync(); |
| 100 | + _logger.LogInformation(4, "User logged out."); |
| 101 | + return Ok(); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments