@page "/userform/edit/{Id}" @using System.Net.Http.Headers; @using System.Text.Json; @using Microsoft.AspNetCore.Components.Forms @inject HttpClient _httpClient @inject NavigationManager Navigation @inject IToastService toastService @inject AuthenticationStateProvider authenticationStateProvider

Editar Usuario

@if (user is not null) {
} @code { [Parameter] public string id { get; set; } = string.Empty; private UserUpdate? user; protected override async Task OnInitializedAsync() { if (id is not null) { user = await GetUser(id); } } public async Task GetUser(string userId) { var customAuthStateProvider = (CustomAuthorizationProvider)authenticationStateProvider; var token = await customAuthStateProvider.GetTokenData(); if (!string.IsNullOrWhiteSpace(token.token)) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.token); try { var response = await _httpClient.GetAsync("/api/Account/GetUserById/" + userId); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var deserializedUser = JsonSerializer.Deserialize(jsonResponse, options); User user = deserializedUser ?? new User(); // user = JsonSerializer.Deserialize(jsonResponse, options); UserUpdate require = new UserUpdate(); require.Id = user.Id; require.UserName = user.UserName; require.Email = user.Email; require.LockoutEnabled = user.LockoutEnabled; require.TwoFactorEnabled = user.TwoFactorEnabled; return require; } } catch { return null; } }; return null; } private async Task UpdateUser() { try { var userJson = JsonSerializer.Serialize(user); var content = new StringContent(userJson, System.Text.Encoding.UTF8, "application/json"); var response = await _httpClient.PutAsync("/api/Account/UpdateUser/" + id, content); if (response.IsSuccessStatusCode) { toastService.ShowSuccess("Usuario actualizado exitosamente"); Navigation.NavigateTo("/users"); } else { var errorMessage = await response.Content.ReadAsStringAsync(); toastService.ShowError(errorMessage); } } catch (Exception ex) { toastService.ShowError(ex.Message); } } public void Cancel() { Navigation.NavigateTo("/users"); } public class User { public string Id { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty; public string NormalizedUserName { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; public string NormalizedEmail { get; set; } = string.Empty; public bool EmailConfirmed { get; set; } public string PasswordHash { get; set; } = string.Empty; public string SecurityStamp { get; set; } = string.Empty; public string? PhoneNumber { get; set; } public bool PhoneNumberConfirmed { get; set; } public bool TwoFactorEnabled { get; set; } public DateTimeOffset? LockoutEnd { get; set; } public bool LockoutEnabled { get; set; } public int AccessFailedCount { get; set; } } public class UserUpdate { public string Id { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty; public string Email { get; set; } = string.Empty; public bool TwoFactorEnabled { get; set; } public bool LockoutEnabled { get; set; } } }