JWT token[Authorize]
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.Password);
→ On stocke hashedPassword dans MongoDB
bool isValid = BCrypt.Net.BCrypt.Verify(input.Password, user.Password);
var token = new JwtSecurityToken(
claims: claims,
expires: DateTime.Now.AddHours(1),
signingCredentials: creds
);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(config["JWT_SECRET"])),
ValidateIssuer = false,
ValidateAudience = false
};
});
app.UseAuthentication();
app.UseAuthorization();
localStorage.setItem("token", response.token);
fetch("/api/add-word", {
method: "POST",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
JWT_SECRET=MaSuperCléUltraSecrète123
MONGO_URI=mongodb://localhost:27017/ma_base
builder.Configuration.AddEnvironmentVariables();
.env