我使用下面的代碼,這是我從jwt-dotnet github page使用JWT-DOTNET以產生有效令牌
private static string CreateToken(UserPrincipal principal)
{
/*
* https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
* http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
*/
var key = ConfigurationManager.AppSettings["jwt-key"];
var claims = new Dictionary<string, string>()
{
{ClaimTypes.Name, "Rainbow Dash" },
{ClaimTypes.WindowsAccountName, "RDash"}
};
var algorithm = new HMACSHA256Algorithm();
var serializer = new JsonNetSerializer();
var urlEncoder = new JwtBase64UrlEncoder();
var encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
var token = encoder.Encode(claims, key);
return token;
}
上面的代碼生成以下令牌最初的借入:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiUmFpbmJvdyBEYXNoIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy93aW5kb3dzYWNjb3VudG5hbWUiOiJSRGFzaCJ9 .5WZWDJ0pvTe6QLjVNUeTfZicX_wSsk1dtYvXUbpiOiw
所以,我跳到jwt.io來測試我的令牌。我被告知我簽名無效。
我如何給它一個有效的'簽名'?我不明白我的智威湯遜錯過了什麼。
Oooooooh!這是要求鑰匙! * facepalm * – Amy
現在我覺得很傻。超時過後,我會接受你的回答。 – Amy
@Amy發生在我們身上:) – Nasreddine