我正在尝试验证Azure AD在AWS Lambda函数中提供的令牌。目前,我有一个MVC网站,您可以使用它对Azure AD进行身份验证,从而返回一个JWT令牌。这个JWT将被传递到一个AWS API网关,在那里Lambda授权器将验证它。
起初,我认为正确的方法是将JWT传回Azure以验证令牌。然而,在阅读这之后,我似乎需要解密令牌,并验证发行者和受众。这就引出了这,它成功地验证了令牌。但是,如果我将mySecret更改为与Azure中配置的不匹配,那么它仍然成功验证了吗?
var authToken = "JWTToken";
string key = "I thought this needed to be the client secret in Azure AD but any string will still pass verification";
string myTenant = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var myAudience = "api://xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var myIssuer = string.Format(CultureInfo.InvariantCulture, "https://sts.windows.net/{0}/", myTenant);
var mySecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key));
var stsDiscoveryEndpoint = String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}/.well-known/openid-configuration", myTenant);
var configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever());
var config = await configManager.GetConfigurationAsync();
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters
{
ValidAudience = myAudience,
ValidIssuer = myIssuer,
IssuerSigningKeys = config.SigningKeys,
ValidateLifetime = false,
IssuerSigningKey = mySecurityKey,
ValidateAudience = true,
ValidateIssuer = true,
};
var validatedToken = (SecurityToken)new JwtSecurityToken();
// Throws an Exception as the token is invalid (expired, invalid-formatted, etc.)
tokenHandler.ValidateToken(authToken, validationParameters, out validatedToken);我已经将Azure配置为这,其中客户端是MVC网站,服务是Lambda。TLDR:这基本上是两个客户端注册,其中Lambda有一个公开的API,MVC网站有一个客户端秘密。
采取了正确的方法吗?如果是的话,我如何解决我所面临的问题?
如能提供任何帮助,将不胜感激。
发布于 2022-02-22 11:34:20
我用过这样的方法:
string authority = "https://login.microsoftonline.com/<your-tenant-id>/";
string clientId = "<your-client-id>";
IConfigurationManager<OpenIdConnectConfiguration> configurationManager =
new ConfigurationManager<OpenIdConnectConfiguration>($"{authority}.well-known/openid-configuration", new OpenIdConnectConfigurationRetriever());
OpenIdConnectConfiguration openIdConfig = await configurationManager.GetConfigurationAsync(CancellationToken.None);
var validationParams = new TokenValidationParameters
{
ValidAudience = clientId,
IssuerSigningKeys = openIdConfig.SigningKeys
};它使用权限下载有效的发行者,签名密钥等。
https://stackoverflow.com/questions/71220220
复制相似问题