2015-11-05 68 views
5

我有一個用例,我希望我的新應用程序(這是身份驗證的來源)爲傳統應用程序提供JWT。編碼JWT令牌在ASP.NET 5中使用System.IdentityModel.Tokens.Jwt

我正在嘗試使用該軟件包,System.IdentityModel.Tokens.Jwt 5.0.0-beta7(如果需要,我可以輕鬆移動到beta8)。創建令牌很簡單,但我在正確簽名時遇到問題。這是我目前的代碼:

Claim[] claims = { 
    new Claim(ClaimTypes.Email, "[email protected]"), 
    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帖子,說support for symmetric keys does not yet exist。但是,在我的搜索中,我看到了圖書館的廣泛使用。特別是我看到了使用InMemorySymmetricSecurityKey。但是,當我嘗試自己使用它時,無法在任何命名空間中找到它,所以我有點困惑,因爲我從這裏開始。

這是一個冗長的解釋,基本上問 - 我如何正確簽署智威湯遜與一個簡單的祕密?

回答

3

當我們轉到新版本的CoreClr時,我們不得不暫時放棄對HMAC的支持。我們選擇在RC1中添加對ECDSA的支持,並計劃在RC2中添加HMAC。

對不起。您可以添加自己的SignatureProvider或等待幾個星期。

+0

感謝您的快速響應。我懷疑是這樣的,但想確認一下。我將使用另一個實現,並在RC2之後回來。再次感謝! – JasCav