@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
Lista de Usuarios
Registrar usuario
@if (users != null && users.Count > 0)
{
| Id |
Username |
Email |
Confirmed |
2FA |
Access Failed |
Lockout |
Actions |
@foreach (var user in users)
{
| @user.Id |
@user.UserName |
@user.Email |
@user.EmailConfirmed |
@user.TwoFactorEnabled |
@user.AccessFailedCount |
@user.LockoutEnabled |
@if (user.UserName.ToLower() != "superdmin")
{
}
|
}
}
else
{
Cargando informacion...
}
@code {
private List users = new List();
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>(jsonResponse, options);
users = deserializedUsers ?? new List();
}
}
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>(jsonResponse, options);
// users = deserializedUsers ?? new List();
// StateHasChanged();
// }
// }
public async Task RefreshAll()
{
var customAuthStateProvider = (CustomAuthorizationProvider)authenticationStateProvider;
var token = await customAuthStateProvider.GetTokenData();
if (!string.IsNullOrWhiteSpace(token.token))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.token);
}
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>(jsonResponse, options);
users = deserializedUsers ?? new List();
StateHasChanged();
}
}
public void EditUser(string userId)
{
navigation.NavigateTo($"/userform/edit/{userId}");
}
public async Task RecoveryPassword(string email)
{
var customAuthStateProvider = (CustomAuthorizationProvider)authenticationStateProvider;
var token = await customAuthStateProvider.GetTokenData();
if (!string.IsNullOrWhiteSpace(token.token))
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.token);
}
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);
}
}
// 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("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; }
}
}