Leandro Hernan Rojas c80da65dac
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 5m47s
Patch JWT
2025-04-28 18:43:59 -03:00

162 lines
5.9 KiB
Plaintext

@page "/users"
@using System.Net.Http.Headers;
@using System.Text.Json;
@inject HttpClient _httpClient
@inject NavigationManager navigation
@inject IToastService toastService
@inject IModalService modalService
@inject AuthenticationStateProvider authenticationStateProvider
<h1>Lista de Usuarios</h1>
<a href="/registration" class="btn btn-dark">Registrar usuario</a>
<br />
@if (users != null && users.Count > 0)
{
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Username</th>
<th>Email</th>
<th>Confirmed</th>
<th>2FA</th>
<th>Access Failed</th>
<th>Lockout</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var user in users)
{
<tr>
<td>@user.Id</td>
<td>@user.UserName</td>
<td>@user.Email</td>
<td>@user.EmailConfirmed</td>
<td>@user.TwoFactorEnabled</td>
<td>@user.AccessFailedCount</td>
<td>@user.LockoutEnabled</td>
<td>
<button class="btn btn-primary btn-margin" @onclick="() => EditUser(user.Id)"> <span class="fa fa-pencil"></span> </button>
@if (user.UserName.ToLower() != "superdmin")
{
<button class="btn btn-danger btn-margin" @onclick="() => ConfirmDelete(user.Id)"> <span class="fa fa-trash"></span> </button>
}
<button class="btn btn-warning btn-margin" @onclick="() => RecoveryPassword(user.Email)"> <span class="fa fa-user-secret"></span> </button>
</td>
</tr>
}
</tbody>
</table>
}
else
{
<br />
<p>Cargando informacion...</p>
}
@code {
private List<User> users = new List<User>();
protected override async Task OnInitializedAsync()
{
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/GetAllUsers");
Console.WriteLine(token.token);
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var deserializedUsers = JsonSerializer.Deserialize<List<User>>(jsonResponse, options);
users = deserializedUsers ?? new List<User>();
}
}
catch (Exception ex)
{
toastService.ShowError(ex.Message);
}
}
}
private async Task HandleDeleteConfirmed()
{
await RefreshAll();
}
public async Task RefreshAll()
{
var response = await _httpClient.GetAsync("/api/Account/GetAllUsers");
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var deserializedUsers = JsonSerializer.Deserialize<List<User>>(jsonResponse, options);
users = deserializedUsers ?? new List<User>();
StateHasChanged();
}
}
public void EditUser(string userId)
{
navigation.NavigateTo($"/userform/edit/{userId}");
}
public async Task RecoveryPassword(string email)
{
string url = $"api/Authentication/forgot-password?email={email}";
var response = await _httpClient.PostAsync(url,null);
var message = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
toastService.ShowSuccess(message);
}
else
{
toastService.ShowError(message);
};
}
private void ConfirmDelete(string userId)
{
var parameters = new ModalParameters()
.Add(nameof(DeleteUser.id), userId)
.Add(nameof(DeleteUser.OnDeleteConfirmed), EventCallback.Factory.Create(this, HandleDeleteConfirmed));
modalService.Show<DeleteUser>("Confirmar Eliminación de Usuario", parameters);
}
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;
}
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; } = string.Empty;
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; }
}
}