Azure AD Jwt token 验证无互联网

标签 azure authentication .net-core jwt azure-active-directory


我有 .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 和加密方法的信息。

enter image description here

Note:alg claim indicates the algorithm that was used to sign the token, while the kid 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 .

enter image description here

然后在 jwt.io 中,将 x5c 值粘贴到公钥框中,格式如下:

enter image description here

格式如下: enter image description here

详情您可以阅读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/

相关文章:

azure - 解释 - 通过 Microsoft Azure DevOps Pipelines 部署生产 NextJS 应用程序时出现的问题

c# - 将实体数据更新到Azure存储表

javascript - 保留 Azure 移动服务的自定义 API 调用的用户身份验证

javascript - 如何修复我的 React 应用程序中的 eslint 错误 'pendo is not defined'

c# - 引用 Razor 类库时重复的基本路径

c# - 运行 Windows 服务时拒绝访问

Azure 服务结构 - 单个服务的性能遥测

cookies - 您可以延长 BC 中登录的 Cookie 持续时间吗?

c# - 为什么我的 Azure AD 身份验证在登录时显示 "The system cannot find the file specified"错误?

asp.net-web-api - .NET Core json 动态属性序列化 (ExpandoObject)