1 Replies - 648 Views - Last Post: 06 September 2019 - 09:15 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

How to generate access token by using jwt ?

Posted 31 August 2019 - 08:46 PM

I work on asp.net core 2.2 application and i need to generate access token by using jwt
i dont know how to generate i search more on internet but not understand what this mean
i need actually when make success login generate access token but i dont know what this term made and how to generate ?
can you please show me any sample code or explain that ?



{
"request_status" : {
"status" : "succeeded",
"code": 0,
"message" : "login succeeded!"
},
"access_token" : "",
"user_data": { 
  "UserFullName" : "Ahmed", 
  "LoginTime" : "12", 
  "Admin" : "adil"
  
},
"branches": [
{ 
  "BranchCode" : "1",
  "BranchName":"Baha" 
  
}
]
}

This post has been edited by ahmedba: 31 August 2019 - 08:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to generate access token by using jwt ?

#2 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: How to generate access token by using jwt ?

Posted 06 September 2019 - 09:15 PM

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 to add userid to payload in case of userid not null

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)
        {
            
            //=================This Setting Related To generate Access Token Data===============
            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);
           
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1