我正在尝试生成一个JWT,用作HTTP请求的Authorization标头。
这是在C#中发生的,使用Nuget的onChangePhotos = async (e) => {
e.preventDefault()
let files = e.target.files
if(files.length === 0) { return }
this.setState({
uploading: true
})
let promises = []
for(const file of files){
const uploadPhotoPromise = await UploadApi.singlePhoto(file)
promises.push(uploadPhotoPromise)
}
try {
const responses = await Promise.all(promises)
let next_photos = []
for(const [index, response] of responses.entries()) {
const { upload_id, url } = response
next_photos.push({photo_id: upload_id, url})
if(responses.length === index + 1){
this.setState({
uploading: false
})
}
}
this.setState({
selectedAlbum: {
...this.state.selectedAlbum,
photos: [
...this.state.selectedAlbum.photos,
...next_photos
]
}
})
} catch(err) {
this.setState({ uploading: false })
alert('failed to upload! Error: ' + err)
}
}
包。我的代码如下:
System.IdentityModel.Tokens.Jwt
最后byte[] certByteArray = Convert.FromBase64String("CertificatePrivateKeyPlaceholder");
var certificate = new X509Certificate2(certByteArray, "MyPassword");
var tokenHandler = new JwtSecurityTokenHandler();
var descriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim("Issuer", "MyIssuer"),
new Claim("Audience", "MyAudience")
}),
EncryptingCredentials = new X509EncryptingCredentials(certificate),
SigningCredentials = new X509SigningCredentials(certificate, SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest),
};
var token = tokenHandler.CreateToken();
string stringToken = "bearer " + tokenHandler.WriteToken(token)
看起来像JWT,但格式为stringToken
,并且看起来是签名部分(在第二个aaaa.bbbb.
之后丢失)
关于我可能做错什么的任何指示?
答案 0 :(得分:1)
您需要将描述符作为参数传递给CreateToken
:
var token = tokenHandler.CreateToken(descriptor);