Генерация токена (находится в моем AuthController):
Код: Выделить всё
private string GenerateJwtToken(ApplicationUser user)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = _configuration["Jwt:Key"] ?? "";
var keyBytes = Encoding.ASCII.GetBytes(key);
var userRoles = _userService.GetIdentityRole(user).Result;
var claims = new List
{
new Claim(ClaimTypes.Name, user.Email ?? string.Empty),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
};
claims.AddRange(userRoles.Select(role => new Claim(ClaimTypes.Role, role)));
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(keyBytes), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
Код: Выделить всё
// Configure JWT Authentication
var key = Encoding.ASCII.GetBytes("");
builder.Services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
Я не уверен, нужна ли какая-либо другая конфигурация или я пропустил шаг для проверки токена JWT?
Спасибо
ИЗМЕНИТЬ
После некоторой отладки я получил это в своей консоли:
Код: Выделить всё
Authentication failed: IDX14100: JWT is not well formed, there are no dots (.).Код: Выделить всё
The token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EndcodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.Код: Выделить всё
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6InNpbW9uLnNqb28yMUBnbWFpbC5jb20iLCJuYW1laWQiOiJkYzhkOTRjZS1hMDBhLTQ5ZTUtOTMzNS0wYzMyY2E1MGRmYjciLCJyb2xlIjoiUmVnaXN0ZXJlZCIsIm5iZiI6MTcyNTYzMzI2NCwiZXhwIjoxNzI1NjM2ODY0LCJpYXQiOjE3MjU2MzMyNjR9.IxmR4FiakEeFjJz6KYRrUNtK2Cqf0gh_C88WRf_MoA4Подробнее здесь: https://stackoverflow.com/questions/789 ... ion-in-net
Мобильная версия