Is there any JSON Web Token (JWT) example in C#?

前端 未结 9 2497
悲哀的现实
悲哀的现实 2020-11-28 01:10

I feel like I\'m taking crazy pills here. Usually there\'s always a million library and samples floating around the web for any given task. I\'m trying to implement authenti

9条回答
  •  伪装坚强ぢ
    2020-11-28 01:55

    Here is the list of classes and functions:

    open System
    open System.Collections.Generic
    open System.Linq
    open System.Threading.Tasks
    open Microsoft.AspNetCore.Mvc
    open Microsoft.Extensions.Logging
    open Microsoft.AspNetCore.Authorization
    open Microsoft.AspNetCore.Authentication
    open Microsoft.AspNetCore.Authentication.JwtBearer
    open Microsoft.IdentityModel.Tokens
    open System.IdentityModel.Tokens
    open System.IdentityModel.Tokens.Jwt
    open Microsoft.IdentityModel.JsonWebTokens
    open System.Text
    open Newtonsoft.Json
    open System.Security.Claims
        let theKey = "VerySecretKeyVerySecretKeyVerySecretKey"
        let securityKey = SymmetricSecurityKey(Encoding.UTF8.GetBytes(theKey))
        let credentials = SigningCredentials(securityKey, SecurityAlgorithms.RsaSsaPssSha256)
        let expires = DateTime.UtcNow.AddMinutes(123.0) |> Nullable
        let token = JwtSecurityToken(
                        "lahoda-pro-issuer", 
                        "lahoda-pro-audience",
                        claims = null,
                        expires =  expires,
                        signingCredentials = credentials
            )
    
        let tokenString = JwtSecurityTokenHandler().WriteToken(token)
    

提交回复
热议问题