繁体   English   中英

验证 Azure AD 令牌

[英]Validating Azure AD token

我有一个 angular 应用程序,我添加了 Azure 活动目录身份验证,我得到了 azure 以返回带有用户详细信息的访问令牌。

我将广告令牌传递给我的 web api (在 .net 4.6.1 中运行)以检查该用户是否已经注册并且我想验证该令牌是否有效。

我使用了以下代码。 但是每当我尝试从 ConfigurationManager 恢复我的配置时,我都会收到错误消息

string tenantId = "someguid"; //not including these guids in here
var audience = "anotherguid";
var issuer = $"https://login.microsoftonline.com/{tenantId}/v2.0";
var stsDiscoveryEndpoint = $"https://login.microsoft.com/{tenantId}/v2.0/.well-known/openid-configuration";
var configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever());
try
{
    CancellationToken cancellationToken = new CancellationToken();
    var config = await configManager.GetConfigurationAsync(cancellationToken);

    var tokenHandler = new JwtSecurityTokenHandler();

    var validationParameters = new TokenValidationParameters
    {
        ValidAudience = audience,
       
        ValidIssuer = issuer,
        IssuerSigningKeys = config.SigningKeys,
        ValidateAudience = true,
        ValidateIssuerSigningKey = true,
        RequireExpirationTime = true,
        ValidateLifetime = true
    };

    SecurityToken validatedToken = new JwtSecurityToken();
    tokenHandler.ValidateToken(token, validationParameters, out validatedToken);

    return validatedToken;
  }

IOException:IDX20807:无法从以下位置检索文档:“https://login.microsoft.com/145fa4fc-d5f6-489c-affn-6407cca77ef0/v2.0/.well-known/openid-configuration”。 HttpResponseMessage:'StatusCode:400,ReasonPhrase:'错误请求',版本:1.1,内容:System.Net.Http.StreamContent,标题:

我是否在 azure 中遗漏了某些内容,还是应该以其他方式获取配置? 任何方向将不胜感激。

  • 错误IDX20807: Unable to retrieve document from: 'https://login.microsoft.com/145xxxxxxx7ccxxxxf0/v2.0/.well-known/openid-configuration ://login.microsoft.com/145xxxxxxx7ccxxxxf0/v2.0/.well-known/openid-configuration 表示:OIDC 元数据https://login.microsoftonline.com/。 well-known/openid-configuration is not valid 由于web configuration有问题或在应用程序中未正确配置。可能是authority参数未正确识别。

I see you have given wrong metadata url in place of https://login.microsoftonline.com as I see you have given wrong metadata url in place of https://login.microsoft.com

在此处输入图像描述

您还需要在 appsettings.json 中添加“权限”。 这是元数据 url 否则必须给出实例和域

(权限格式:“ https://login.microsoftonline.com/<tenantId>"

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "yourtenantdomain",
    "ClientId": "My Client Id",
    "TenantId": "<common or organizations>", // must be there in Multi Tenant application 
    "CallbackPath": "/signin-oidc"
  },

并尝试使用 dotnet 版本 4.7 或更高版本,并确保在 tls 版本为 1.2 或更高版本上运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM