roadie/RoadieApi/Controllers/AccountController.cs

143 lines
5.1 KiB
C#
Raw Normal View History

2018-11-02 21:04:49 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Roadie.Api.Models;
using Roadie.Api.Services;
using Roadie.Library.Identity;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("auth")]
[ApiController]
[AllowAnonymous]
public class AccountController : ControllerBase
{
private readonly IConfiguration configuration;
private readonly ILogger<AccountController> logger;
private readonly SignInManager<ApplicationUser> signInManager;
private readonly ITokenService tokenService;
private readonly UserManager<ApplicationUser> userManager;
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IConfiguration configuration,
ILogger<AccountController> logger,
ITokenService tokenService)
{
this.userManager = userManager;
this.signInManager = signInManager;
this.configuration = configuration;
this.logger = logger;
this.tokenService = tokenService;
}
[HttpPost]
[Route("token")]
public async Task<IActionResult> CreateToken([FromBody]LoginModel model)
{
if (ModelState.IsValid)
{
try
{
// Login user
var loginResult = await signInManager.PasswordSignInAsync(model.Username, model.Password, isPersistent: false, lockoutOnFailure: false);
if (!loginResult.Succeeded)
{
return BadRequest();
}
// If successful login return OK with generated token
var user = await userManager.FindByNameAsync(model.Username);
2018-11-10 23:26:04 +00:00
var t = await this.tokenService.GenerateToken(user, this.userManager);
return Ok(t);
2018-11-02 21:04:49 +00:00
}
catch (Exception ex)
{
this.logger.LogError(ex, "Eror in CreateToken");
return BadRequest();
}
}
return BadRequest(ModelState);
}
[Authorize]
[HttpPost]
[Route("refreshtoken")]
public async Task<IActionResult> RefreshToken()
{
var username = User.Identity.Name ??
User.Claims.Where(c => c.Properties.ContainsKey("unique_name")).Select(c => c.Value).FirstOrDefault();
if (!String.IsNullOrWhiteSpace(username))
{
var user = await userManager.FindByNameAsync(username);
2018-11-10 23:26:04 +00:00
return Ok(await this.tokenService.GenerateToken(user, this.userManager));
2018-11-02 21:04:49 +00:00
}
else
{
ModelState.AddModelError("Authentication", "Authentication failed!");
return BadRequest(ModelState);
}
}
[HttpPost]
[Route("register")]
[AllowAnonymous]
public async Task<IActionResult> Register([FromBody] RegisterModel registerModel)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = registerModel.Username,
Email = registerModel.Email,
CreatedDate = DateTime.UtcNow
};
var identityResult = await this.userManager.CreateAsync(user, registerModel.Password);
if (identityResult.Succeeded)
{
await signInManager.SignInAsync(user, isPersistent: false);
2018-11-10 23:26:04 +00:00
return Ok(this.tokenService.GenerateToken(user, this.userManager));
2018-11-02 21:04:49 +00:00
}
else
{
return BadRequest(identityResult.Errors);
}
}
return BadRequest(ModelState);
}
[HttpPost]
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordModel resetPasswordModel)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = resetPasswordModel.Username,
Email = resetPasswordModel.Email,
CreatedDate = DateTime.UtcNow
};
var identityResult = await this.userManager.ResetPasswordAsync(user, resetPasswordModel.Token, resetPasswordModel.Password);
if (identityResult.Succeeded)
{
await signInManager.SignInAsync(user, isPersistent: false);
2018-11-10 23:26:04 +00:00
return Ok(this.tokenService.GenerateToken(user, this.userManager));
2018-11-02 21:04:49 +00:00
}
else
{
return BadRequest(identityResult.Errors);
}
}
return BadRequest(ModelState);
}
}
}