0% found this document useful (0 votes)
12 views4 pages

/// chỉnh trong appsetting.json

Uploaded by

tuannmhe179004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

/// chỉnh trong appsetting.json

Uploaded by

tuannmhe179004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

/// chỉnh trong appsetting.

json

"Jwt": {

"Issuer": "https://localhost:5000",

"Audience": "User",

"JwtSecurityKey": "this is my custom Secret key for authnetication ",

"JwtExpiryInMinutes": 30,

"AllowedHosts": "*"

/// hàm viết GenerateToken

public string GenerateToken(string email, string role)

var key = new


SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:JwtSecurityKey"]));

var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var expiry = DateTime.UtcNow.AddMinutes(int.Parse(_configuration["Jwt:JwtExpiryInMinutes"]));

var claims = new[]

new Claim(ClaimTypes.Email, email),

new Claim(ClaimTypes.Role, role),

new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())

};

var token = new JwtSecurityToken(

issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],

claims: claims,

expires: expiry,

signingCredentials: creds

);

return new JwtSecurityTokenHandler().WriteToken(token);

/// Chỉnh trong file Program.cs

builder.Services.AddAuthentication(options =>

options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

})

.AddJwtBearer(options =>

options.SaveToken = true;

options.RequireHttpsMetadata = false;

options.TokenValidationParameters = new TokenValidationParameters

ValidateIssuer = true,

ValidateAudience = true,

ValidAudience = builder.Configuration["Jwt:Audience"],

ValidIssuer = builder.Configuration["Jwt:Issuer"],

IssuerSigningKey = new
SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:JwtSecurityKey"]))
};

});

// Nếu muốn hiện Authoriza lên web thì có thể them vào program.cs

builder.Services.AddSwaggerGen(option =>

option.SwaggerDoc("v1", new OpenApiInfo { Title = "Book API", Version = "v1" });

option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme

In = ParameterLocation.Header,

Description = "Please enter a valid token",

Name = "Authorization",

Type = SecuritySchemeType.Http,

BearerFormat = "JWT",

Scheme = "Bearer"

});

option.AddSecurityRequirement(new OpenApiSecurityRequirement

new OpenApiSecurityScheme

Reference = new OpenApiReference

Type=ReferenceType.SecurityScheme,

Id="Bearer"

},
new string[]{}

});

});

You might also like