У меня проблема с хранением утверждений, когда я использую @context.User.Identity.Name для получения значения, но его не удается найти. Я пробовал каждый метод, но он остается прежним. Я не знаю, где я ошибся. Этот код создает токен Jwt в методе класса AuthenticationStaffOnly с именем GenerateJwtToken. После этого он будет отправлен в Login.razor для хранения в LocalStorage. Затем вызывается GetAuthenticationStateAsync, чтобы проверить, действителен ли токен Jwt, а затем сохранить его в утверждении и перейти на другие страницы с атрибутом [Authorize(Roles = "Admin,Staff")].
<AuthorizeView>
<Authorized>
<h1>Hello, @context.User.Identity?.Name </h1>
</Authorized>
<NotAuthorized>
@context.User.Identity.Name
@string.Join(",", ((ClaimsIdentity)context.User.Identity).Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value))
<p>Your email is: @context.User.FindFirstValue("email")</p>
<h1>คุณยังไม่ได้เข้าสู่ระบบ หรือ ไม่มีสิทธิเข้าถึงหน้านี้</h1>
</NotAuthorized>
</AuthorizeView>
using ComEsport.Models;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace ComEsport.Services
{
public class AuthenticationStaffOnly : AuthenticationStateProvider
{
private readonly ILogger<StaffService> _logger;
private readonly IConfiguration _configuration;
private readonly ILocalStorageService _localStorage;
private readonly HttpClient _http;
private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private readonly IHttpContextAccessor _httpContextAccessor;
public AuthenticationStaffOnly(ILogger<StaffService> logger, IConfiguration configuration, ILocalStorageService localStorage, HttpClient http, JwtSecurityTokenHandler jwtSecurityTokenHandler, IHttpContextAccessor httpContextAccessor)
{
_logger = logger;
_configuration = configuration;
_localStorage = localStorage;
_http = http;
_jwtSecurityTokenHandler = jwtSecurityTokenHandler;
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// สำหรับการสร้าง JwtToken และลงทะเบียน
/// </summary>
/// <param name = "staff"></param>
/// <returns></returns>
public async Task<string?> GenerateJwtToken(Staff staff)
{
if (staff.StaffEmail == null) return null;
byte[] Key = Convert.FromBase64String(_configuration["JWTSettings:SecretKey"]);
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Key);
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Email, staff.StaffEmail),
new Claim(ClaimTypes.Name, staff.StaffFirstName + staff.StaffLastName),
new Claim(ClaimTypes.Role, staff.StaffPermission)
}),
Expires = DateTime.UtcNow.AddMinutes(25),
SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature)
};
JwtSecurityToken token = _jwtSecurityTokenHandler.CreateJwtSecurityToken(descriptor);
_logger.LogInformation("Generate Jwt Token Success from AuthenticationStaffOnly Method GenerateJwtToken");
return _jwtSecurityTokenHandler.WriteToken(token);
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
// ดึง Jwt Token ออกจาก localStorage
string jsonString = await _localStorage.GetItemAsStringAsync("access_token");
// แปลง access_token โดยปกติจะมี Model TokenClass ติดมาด้วยจำเป็นต้องแปลงออก เพื่อจะนำไปอ่าน jwt Token
JObject json = JObject.Parse(jsonString);
string token = json.GetValue("TokenOrMessage").Value<string>();
var TokenData = _jwtSecurityTokenHandler.ReadJwtToken(token);
// สร้าง identity และ claimsPrincipal จาก Jwt Token
var claims = new List<Claim>();
claims.AddRange(TokenData.Claims.Select(claim => new Claim(claim.Type, claim.Value)));
var identity = new ClaimsIdentity(claims, "Jwt");
var claimsPrincipal = new ClaimsPrincipal(identity);
var state = new AuthenticationState(claimsPrincipal);
NotifyAuthenticationStateChanged(Task.FromResult(state));
return state;
}
}
}
Значение @Context равно null и NotAuthorized.





Я могу сделать это, добавив эту строку кода:
_httpContext.User.AddIdentity(identity);
Не удалось передать, не могли бы вы загрузить минимальный пример, который можно было бы репо? Я получаю NotAuthorized только тогда, когда не регистрировал AuthenticationStaffOnly. Но в этом случае при отладке я не попаду в метод GetAuthenticationStateAsync()