parent
d39d5995ab
commit
07be554c66
@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace OAuthServer;
|
||||
|
||||
public class AppDbContext : IdentityDbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Setup Identity roles
|
||||
modelBuilder.Entity<IdentityRole>().HasData(
|
||||
new IdentityRole { Id = Guid.NewGuid().ToString(), Name = "User", NormalizedName = "USER" },
|
||||
new IdentityRole { Id = Guid.NewGuid().ToString(), Name = "External", NormalizedName = "EXTERNAL" }
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
|
||||
|
||||
namespace OAuthServer.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("")]
|
||||
public class LoginController : ControllerBase
|
||||
{
|
||||
private readonly SignInManager<IdentityUser> _signInManager;
|
||||
private readonly UserManager<IdentityUser> _userManager;
|
||||
|
||||
public LoginController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public record RegisterRequest(string Username, [StringLength(1024)] string Password);
|
||||
|
||||
[HttpPost]
|
||||
[Route("register")]
|
||||
public async Task<ActionResult<string>> Register([FromBody] RegisterRequest registerRequest)
|
||||
{
|
||||
IdentityUser user = new IdentityUser
|
||||
{
|
||||
UserName = registerRequest.Username,
|
||||
};
|
||||
|
||||
IdentityResult registerResult = await _userManager.CreateAsync(user, registerRequest.Password);
|
||||
if (!registerResult.Succeeded)
|
||||
{
|
||||
return BadRequest(registerResult);
|
||||
}
|
||||
|
||||
IdentityResult roleResult = await _userManager.AddToRoleAsync(user, "User");
|
||||
if (!roleResult.Succeeded)
|
||||
{
|
||||
throw new Exception($"Adding role User for {registerRequest.Username} not successful: {roleResult}");
|
||||
}
|
||||
|
||||
return Ok("Registered");
|
||||
}
|
||||
|
||||
|
||||
public record LoginRequest(string Username, [StringLength(1024)] string Password);
|
||||
|
||||
[HttpPost]
|
||||
[Route("login")]
|
||||
public async Task<ActionResult> Login([FromBody] LoginRequest loginRequest)
|
||||
{
|
||||
SignInResult result = await _signInManager.PasswordSignInAsync(loginRequest.Username, loginRequest.Password,
|
||||
isPersistent: true, lockoutOnFailure: false);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
if (result.IsLockedOut)
|
||||
{
|
||||
return Unauthorized("Account disabled");
|
||||
}
|
||||
|
||||
return Unauthorized("Username or password invalid");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("logout")]
|
||||
public async Task<ActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return Ok("Successfully logged out");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue