Cola.ColaJwt 1.0.3

dotnet add package Cola.ColaJwt --version 1.0.3                
NuGet\Install-Package Cola.ColaJwt -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cola.ColaJwt" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cola.ColaJwt --version 1.0.3                
#r "nuget: Cola.ColaJwt, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cola.ColaJwt as a Cake Addin
#addin nuget:?package=Cola.ColaJwt&version=1.0.3

// Install Cola.ColaJwt as a Cake Tool
#tool nuget:?package=Cola.ColaJwt&version=1.0.3                

ColaJwt

author: odinsam

{
  "ColaJwt": {
    "Secret": "123456123456123456123456",
    "IsUser": "odinsam.com",
    "Audience": "odinsam",
    "AccessExpiration": 30,
    "RefreshExpiration": 30
  }
}
// 默认使用自定义的 ApiResponseForAuthenticationHandler scheme 重写了 Authenticate、Challenge、Forbid
builder.Services.AddColaJwt(config);
// or
builder.Services.AddColaJwt<ApiResponseForAuthenticationHandler>(config);


app.UseRouting();
// 注意位置和顺序
app.UseAuthentication();
app.UseAuthorization();
// create token
[HttpGet("GetToken")]
public ApiResult  GetToken()
{
    var jwtConfig = _configuration.GetSection("ColaJwt");
    var tokenResult = TokenHelper.CreateToken(
        "odinsam",
        jwtConfig.GetValue<string>("Secret"),
        new ClaimsIdentity(new[]
        {
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
            new Claim(JwtRegisteredClaimNames.Sub, "odinsam")
        }),
        jwtConfig.GetValue<int>("AccessExpiration"),
        jwtConfig.GetValue<int>("RefreshExpiration"));
    dicToken.TryAdd(tokenResult.Refresh.Token, tokenResult.Refresh);
    return new ApiResult
    {
        Code = 0,
        Data = tokenResult
    };
}
//validation token
[Authorize]
    [HttpGet("GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
    }
//验证token
[HttpGet("ValidateToken")]
public ApiResult ValidateToken(string token,string refreshToken)
{
    try
    {
        var jwtConfig = _configuration.GetSection("ColaJwt");
        var secretKey = jwtConfig.GetValue<string>("Secret");
        dicToken.TryGetValue(refreshToken,out var storedRefreshToken);
        var validateResult = TokenHelper.ValidateToken(secretKey, token, storedRefreshToken);
        storedRefreshToken.Used = validateResult;
        return new ApiResult()
        {
            Message = "validate success, refreshToken is used"
        };
    }
    catch
    {
        return null;
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 195 1/4/2024