![](/img/trans.png)
[英]Implementing JWT authentication in Asp.net WebApi using Microsoft System.IdentityModel.Tokens.Jwt
[英]Encoding JWT Token Using System.IdentityModel.Tokens.Jwt in ASP.NET 5
我有一個用例,我希望我的新應用程序(它是身份驗證的來源)為遺留應用程序提供JWT。
我正在嘗試使用該軟件包, System.IdentityModel.Tokens.Jwt 5.0.0-beta7
beta8
如果需要,我可以很容易地轉到beta8
)。 創建令牌很簡單,但我在簽名時遇到問題。 這是我目前的代碼:
Claim[] claims = {
new Claim(ClaimTypes.Email, "test@example.net"),
new Claim(ClaimTypes.Role, "admin"),
new Claim(ClaimTypes.Uri, ""),
new Claim(ClaimTypes.Expiration, DateTime.UtcNow.Add(TimeSpan.FromDays(1)).ToString()),
new Claim("personId", personId.ToString())
};
var key = Convert.FromBase64String("my super secret key goes here");
var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey(key),
SecurityAlgorithms.HMAC_SHA256,
SecurityAlgorithms.Sha256Digest
);
var jwt = new JwtSecurityToken("localhost", "all", claims,
DateTime.UtcNow, DateTime.UtcNow.AddDays(1), signingCredentials);
var handler = new JwtSecurityTokenHandler();
handler.WriteToken(jwt);
return Content($"{handler.WriteToken(jwt)}");
如果我沒有簽署令牌,一切正常。 但是,只要我添加簽名憑據,就會收到不支持HMAC的錯誤。 我確實發現另一個SO帖子說支持對稱密鑰尚不存在 。 但是,在我的搜索中,我看到了圖書館的廣泛使用。 特別是,我看到了InMemorySymmetricSecurityKey
的使用。 但是,當我嘗試自己使用它時,它無法在任何命名空間中找到,所以我有點困惑,因為我從這里開始。
這是一個冗長的解釋基本上要問 - 我如何用一個簡單的秘密正確簽署JWT?
當我們遷移到新版本的CoreClr時,我們不得不暫時放棄對HMAC的支持。 我們選擇在RC1中添加對ECDSA的支持,並計划在RC2中添加HMAC。
對不起,麻煩。 您可以添加自己的SignatureProvider或等待幾周。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.