add controller to access register from url

features/IdentitySvc
Vianney JOURDY 6 days ago
parent 5bb8f98f2f
commit 104ef33aef

@ -0,0 +1,45 @@
using IdentitySvc.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace IdentitySvc.Controllers;
[ApiController]
[Route("api/[controller]")]
public class RegisterApiController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
public RegisterApiController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
[HttpPost]
public async Task<IActionResult> Register([FromBody] RegisterApiDto dto)
{
if (dto.Username != "Harry")
return BadRequest("Invalid registration data.");
var user = new ApplicationUser
{
UserName = dto.Username,
Email = dto.Email,
EmailConfirmed = true
};
var result = await _userManager.CreateAsync(user, dto.Password);
if (!result.Succeeded)
return BadRequest(result.Errors);
return StatusCode(201);
}
}
public class RegisterApiDto
{
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}

@ -10,6 +10,8 @@ Log.Information("Starting up");
try try
{ {
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Host.UseSerilog((ctx, lc) => lc builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
@ -24,6 +26,7 @@ try
// in production you will likely want a different approach. // in production you will likely want a different approach.
SeedData.EnsureSeedData(app); SeedData.EnsureSeedData(app);
app.MapControllers();
app.Run(); app.Run();
} }
catch (Exception ex) when (ex is not HostAbortedException) catch (Exception ex) when (ex is not HostAbortedException)

Loading…
Cancel
Save