我有 .net core Web api,它在公司 Intranet A 的计算机上运行,并且无法访问 Internet。
然后我在 Intranet B 中有一个网站,该网站在 Azure AD 中进行了身份验证。
我想使用 header Authentication: JwtTokenFromAzure
将请求从 B 发送到 A。
验证此 token 的最佳选项是什么?
最佳答案
要验证 id_token 或 access_token,您的应用应验证 token 签名和声明。 您可以使用 jwt.io 手动验证 token 或者您也可以使用该代码。
要手动验证 token ,您应该首先验证 token 的签名。 Azure AD 颁发的 token 使用行业标准非对称加密算法进行签名,JWT header 包含有关用于签署 token 的 key 和加密方法的信息。
Note:
alg
claim indicates the algorithm that was used to sign the token, while thekid
claim indicates the particular public key that was used to sign the token. The v1.0 endpoint returns both the x5t and kid claims, while the v2.0 endpoint responds with only the kid claim.
这是 v1 token header 示例。在这里,您可以使用 kid
值在 jwks_uri 中查找相关的公钥 (x5c
)。通过使用 OpenID Connect metadata document .
然后在 jwt.io 中,将 x5c 值粘贴到公钥框中,格式如下:
详情您可以阅读here .
如果您想使用代码来验证 token ,这里有一个示例:
public JwtSecurityToken Validate(string token)
{
string stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";
ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint);
OpenIdConnectConfiguration config = configManager.GetConfigurationAsync().Result;
TokenValidationParameters validationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ValidateIssuer = false,
IssuerSigningTokens = config.SigningTokens,
ValidateLifetime = false
};
JwtSecurityTokenHandler tokendHandler = new JwtSecurityTokenHandler();
SecurityToken jwt;
var result = tokendHandler.ValidateToken(token, validationParameters, out jwt);
return jwt as JwtSecurityToken;
}
详细代码示例请引用此case .
关于Azure AD Jwt token 验证无互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746141/