@page "/registration" @using System.Text.Json @using System.ComponentModel.DataAnnotations @inject HttpClient httpClient @inject IToastService toastService @inject NavigationManager navigation

Registro de Usuario

@foreach (var role in roles) { }
@code { private User user = new(); private string confirmPassword = string.Empty; public List roles = new(); private const int USERNAME_VALID_LENGTH = 8; protected override async Task OnInitializedAsync() { roles = await GetAllRoles(); } private async Task> GetAllRoles() { var response = await httpClient.GetFromJsonAsync>("/api/Account/GetAllRoles"); return response ?? new List(); } private async Task RegistrarUsuario() { if (user.UserName.Length < USERNAME_VALID_LENGTH) { toastService.ShowError($"El nombre de usuario debe tener al menos {USERNAME_VALID_LENGTH} caracteres."); } else if (user.Password != confirmPassword) { toastService.ShowError("Las contraseñas no coinciden."); } else if (string.IsNullOrEmpty(user.Role)) { toastService.ShowWarning("Debe seleccionar un rol."); } else { await CreateUser(); } } private async Task CreateUser() { var requestContent = new StringContent(JsonSerializer.Serialize(user), System.Text.Encoding.UTF8, "application/json"); var response = await httpClient.PostAsync("/api/Authentication/register/", requestContent); var errorResponse = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { toastService.ShowSuccess(errorResponse); navigation.NavigateTo("/users"); } else { toastService.ShowError(errorResponse); } } private void Cancel() { navigation.NavigateTo("/users"); } public class User { [Required] public string UserName { get; set; } = string.Empty; [EmailAddress, Required] public string EmailAddress { get; set; } = string.Empty; [Required] public string Password { get; set; } = string.Empty; [Required] public string Role { get; set; } = string.Empty; public string? FirstName { get; set; } public string? LastName { get; set; } public string? PhoneNumber { get; set; } public string? Company { get; set; } public string? Department { get; set; } public DateTime? BirthDate { get; set; } } public class Role { public string Id { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; public string NormalizedName { get; set; } = string.Empty; public string ConcurrencyStamp { get; set; } = string.Empty; } }