problem
How to add UserId to payload when generate access Token Using JWT asp.net core 2.2 ?
i make function generate access token but i need to modify it to have or load userid on payload and get result as json ?
how to do that if possible ?
I need if string userid paramters have values add it to payload of generate access token .
What I have tried:
- public string GenerateTokens(string userId)
- {
- var Claims = new Claim[]
- {
- new Claim(JwtRegisteredClaimNames.Sub,userId)
- };
- var signingkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is secret phrase"));
- var SigningCredntials = new SigningCredentials();
- var Jwt = new JwtSecurityToken();
- return new JwtSecurityTokenHandler().WriteToken(Jwt);
- }
- configure service on startup
- public void ConfigureServices(IServiceCollection services)
- {
-
- var signingkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is secret phrase"));
- services.AddAuthentication(options => {
- options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- }).AddJwtBearer(cfg =>
- {
- cfg.RequireHttpsMetadata = false;
- cfg.SaveToken = false;
- cfg.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
- {
- IssuerSigningKey = signingkey,
- ValidateAudience = false,
- ValidateIssuer = false,
- ValidateLifetime = false,
- ValidateIssuerSigningKey = true
- };
- });
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
- }