登录后,我将生成一个令牌并将其保存在存储会话中。令牌的到期时间是 8 分钟,一切都很好。我遇到的问题是,当我上传照片时,例如页面会刷新,并且令牌在 8 分钟后仍然过期,但我想要的是刷新时我想将令牌过期时间重置回 8 分钟或生成新令牌。如何做到这一点?
在登录中我正在 API 中生成令牌:
loginRes.Token = CreateJWT(user);
private string CreateJWT(User user)
{
var secretKey = configuration.GetSection("AppSettings:Key").Value;
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
var claims = new Claim[] {
new Claim(ClaimTypes.Name, user.Username),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
};
var signingCredentials = new SigningCredentials(
key, SecurityAlgorithms.HmacSha256Signature);
var tokenDescriptor = new SecurityTokenDescriptor{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.AddMinutes(8),
SigningCredentials = signingCredentials
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
上面写着
Expires = DateTime.UtcNow.AddMinutes(8),
现在在前端我有一个上传照片的页面,当我这样做时,页面会刷新以显示上传的照片
initializeFileUploader()
{
this.uploader = new FileUploader({
url: this.baseUrl + '/familydocuments/add/photo/' + this.thefolder,
authToken: 'Bearer ' + sessionStorage.getItem('token'),
isHTML5: true,
removeAfterUpload: true,
autoUpload: true,
maxFileSize: this.maxAllowedFileSize
});
this.uploader.onAfterAddingFile = (file) => {
file.withCredentials = false;
};
this.uploader.onSuccessItem = (item,respose,status, header) =>
{
if (respose)
{
const photo = JSON.stringify(respose);
const photos = JSON.parse(photo);
this.allPhotos.push(photos);
}
// setTimeout(()=>
// {
// window.location.reload();
// this.router.navigate(["familydocuments"])
// }, 15000);
};
}
);
我需要生成新令牌或在不重新加载页面的情况下显示照片,该怎么做?
如果全部回答“是”,则使用会话存储中的令牌进行 http 事务。
setTimeout
。当你收到新的令牌时,然后在其他地方更新(会话存储等)