Я запустил две среды разработки с помощью
dotnet watch run --urls=http://localhost:5001Команда .
Один экземпляр генерирует токен PASETO, другой его декодирует, экземпляр декодирования завершается с ошибкой «Неверный токен».
Я использовал следующий код с пакетом nuget paseto-dotnet.
using System.Text.Json;
using FluentResults;
using Paseto;
using Paseto.Builder;
using Paseto.Cryptography.Key;
public static class TokenUtils
{
readonly static PasetoSymmetricKey pasetoSymmetricKey = new PasetoBuilder()
.UseV4(Purpose.Local)
.WithSharedKey(Encoding.UTF8.GetBytes("DummySecret"))
.GenerateSymmetricKey();
public static string GenerateLocalToken(TokenPayload tokenPayload)
{
return new PasetoBuilder()
.UseV4(Purpose.Local)
.WithKey(pasetoSymmetricKey)
.AddClaim("UserId", tokenPayload.AppId)
.IssuedAt(DateTime.UtcNow)
.Expiration(tokenPayload.ExpiresAt)
.Encode();
}
public static Result DecodeLocalToken(string token)
{
try
{
PasetoTokenValidationResult decodedResult = new PasetoBuilder()
.UseV4(Purpose.Local)
.WithSharedKey(Encoding.UTF8.GetBytes("DummySecret"))
.WithKey(pasetoSymmetricKey)
.Decode(token);
if (decodedResult is null) { return Result.Fail("Failed to Decode Token"); }
if (!decodedResult.IsValid) { return Result.Fail("Invalid Token"); }
TokenPayload? tokenPayload = JsonSerializer.Deserialize(decodedResult.Paseto.RawPayload);
if (tokenPayload is null) { return Result.Fail("Failed to Deserialize TokenPayload"); }
return Result.Ok(tokenPayload);
}
catch (Exception exception)
{
return Result.Fail(exception.Message);
}
}
}
using System.Text.Json.Serialization;
public class TokenPayload
{
public string UserId { get; set; } = default!;
[JsonPropertyName("iat")]
public DateTime IssuedAt { get; set; } = default!;
[JsonPropertyName("exp")]
public DateTime ExpiresAt { get; set; } = default!;
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... otnet-nuge
Как создать согласованный PasetoSymmetricKey с пакетом nuget adavidesmet/paseto-dotnet? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Проблема с пакетом nuget NetBarcode 1.7.2 и функцией SixLabors.Fonts.Measure().
Anonymous » » в форуме C# - 0 Ответы
- 12 Просмотры
-
Последнее сообщение Anonymous
-