【发布时间】:2014-10-15 13:17:48
【问题描述】:
各位程序员,
我目前正在为 Web Api 2 中的 Microsoft 帐户 JWT 令牌验证而苦苦挣扎。 我已经找到了 OWIN 中间件(NuGet 包 Microsoft.Owin.Security.Jwt),这是我的 Startup.cs 中配置的代码:
public void ConfigureAuth(IAppBuilder app)
{
var sha256 = new SHA256Managed();
var secretBytes = System.Text.Encoding.UTF8.GetBytes(@"(My app client secret)" + "JWTSig");
byte[] signingKey = sha256.ComputeHash(secretBytes);
app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
AllowedAudiences = new[] { "(My API's domain )" },
IssuerSecurityTokenProviders =
new[]
{
new SymmetricKeyIssuerSecurityTokenProvider(
"urn:windows:liveid", signingKey)
}
});
}
我在这里找到了 sn-p:
http://code.lawrab.com/2014/01/securing-webapi-with-live-id.html
JWT 令牌是使用 Live SDK 从我的 Windows 应用商店应用客户端发送的。我正在发送身份验证令牌,而不是访问令牌,所以我确定它是 JWT。使用像这样的在线调试器:http://jwt.io/ 我能够成功解码标头和有效负载部分,但我找不到验证签名的方法。发送带有该 JWT 的请求时,我的 Web API 的调试输出是:
Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 错误:0:身份验证失败 System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException:IDX10500:签名验证失败。无法解析 SecurityKeyIdentifier:'SecurityKeyIdentifier ( IsReadOnly = 假, 计数 = 1, 子句[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause ) ', 令牌:'{"alg":"HS256","kid":"0","typ":"JWT"}.{"ver":1,"iss":"urn:windows:liveid","exp ":1408666611,"uid":"我的 Microsoft 帐户 uid","aud":"(我的 API 的域)","urn:microsoft:appuri":"ms-app://(客户端应用商店 id)", "urn:microsoft:appid":"(来自 account.live.com/developers 的应用 ID)"} 原始数据:(JWT 令牌)'。 w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParameters 验证参数) w System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(字符串安全令牌,令牌验证参数验证参数,安全令牌和验证令牌) w Microsoft.Owin.Security.Jwt.JwtFormat.Unprotect(String protectedText) w Microsoft.Owin.Security.Infrastructure.AuthenticationTokenReceiveContext.DeserializeTicket(String protectedData) w Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.d__0.MoveNext()对不起我的英语,任何更正都非常受欢迎。
【问题讨论】:
标签: c# security owin asp.net-web-api2 jwt