@page "/registration" @using System.Text.Json; @using System.Collections.Generic @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 User(); private string confirmPassword=string.Empty; public List roles = new List(); // Reemplaza con tu lista de roles reales 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($"longitud de nombre minima: {USERNAME_VALID_LENGTH} caracteres. ¡Inténtalo de nuevo!"); } else if (user.Password != confirmPassword) { toastService.ShowError("Las contraseñas no coinciden. ¡Inténtalo de nuevo!"); } else if (string.IsNullOrEmpty(user.Role)) { toastService.ShowWarning("Debes seleccionar un rol."); } else { await CreateUser(); } } public 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); } } public void Cancel() { navigation.NavigateTo("/users"); } public class User { [Required(ErrorMessage = "El Username es un dato obligatorio")] public string UserName { get; set; } = string.Empty; [EmailAddress, Required(ErrorMessage = "El correo electronico es un dato obligatorio")] public string EmailAddress { get; set; } = string.Empty; [Required(ErrorMessage = "La contraseña es un dato obligatorio")] public string Password { get; set; } = string.Empty; [Required(ErrorMessage = "El rol es un dato obligatorio")] public string Role { get; set; } = string.Empty; } 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; } }