問(wèn)題描述
我正在使用 JwtTokens 并且無(wú)法使它們正常工作.我正在使用 http://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/ 為它.我知道代碼是一團(tuán)糟,但只是為了展示我正在嘗試做的事情.問(wèn)題是我希望 JwtTokenHandler 因?yàn)樯芷诙鵁o(wú)法通過(guò)驗(yàn)證.
I'm playing a with JwtTokens and can't make them work properly. I'm using http://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/ for it. I know the code is a mess but is just to show what I'm trying to do. The problem is that I want the JwtTokenHandler to fail the validation because of the lifetime.
var key = "5A0AB091-3F84-4EC4-B227-0834FCD8B1B4";
var domain = "http://localhost";
var allowedAudience = "http://localhost";
var signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
var digestAlgorithm = "http://www.w3.org/2001/04/xmlenc#sha256";
var issuer = "self";
var securityKey = System.Text.Encoding.Unicode.GetBytes(key);
var inMemorySymmetricSecurityKey = new InMemorySymmetricSecurityKey(securityKey);
var now = DateTime.UtcNow;
var expiry = now.AddSeconds(1);
var tokenHandler = new JwtSecurityTokenHandler();
var claimsList = new List<Claim>()
{
new Claim(ClaimTypes.Name, "user"),
new Claim(ClaimTypes.Webpage, allowedAudience),
new Claim(ClaimTypes.Uri, domain),
new Claim(ClaimTypes.Expiration,expiry.Ticks.ToString())
};
var roles = new List<string>() { "admin" };
claimsList.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));
var identity = new GenericIdentity("user");
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(identity, claimsList),
TokenIssuerName = issuer,
AppliesToAddress = allowedAudience,
Lifetime = new Lifetime(now, expiry),
SigningCredentials = new SigningCredentials(inMemorySymmetricSecurityKey, signatureAlgorithm, digestAlgorithm),
};
var token = tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor));
var validationParameters = new TokenValidationParameters()
{
ValidIssuer = issuer,
ValidAudience = allowedAudience,
IssuerSigningToken = new BinarySecretSecurityToken(securityKey)
};
Thread.Sleep(2000);
try
{
SecurityToken securityToken;
tokenHandler.ValidateToken(token, validationParameters, out securityToken);
Console.WriteLine("OK");
}
catch (Exception e)
{
Console.WriteLine("Error {0}", e.Message);
}
由于我等待 2 秒,這不是假設(shè)會(huì)失敗嗎?如果我將 ValidationTokenParameter 的頒發(fā)者更改為x",它會(huì)失敗...
Isn't this suppose to fail since I'm waiting 2 seconds? It fails if I change the issuer of the ValidationTokenParameter to "x"...
推薦答案
發(fā)現(xiàn)問(wèn)題.驗(yàn)證參數(shù)的默認(rèn)時(shí)鐘偏差為 5 分鐘
Found the issue. The validation parameters have a default clock skew of 5 minutes
/// <summary>
/// Default for the clock skew.
///
/// </summary>
///
/// <remarks>
/// 300 seconds (5 minutes).
/// </remarks>
public static readonly TimeSpan DefaultClockSkew;
將其設(shè)置為 0 使這項(xiàng)工作.仍然不明白為什么傾斜是 5 分鐘,如果我將到期時(shí)間設(shè)置在某個(gè)時(shí)間點(diǎn)!!!
Setting that to 0 make this work. Still don't understand why the skew is 5 minutes, if I set the expiry at some point!!!
這篇關(guān)于在 C# 中正確使用 JwtTokens的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!