gpt4 book ai didi

azure-active-directory - 从元数据端点缓存颁发者和 key

转载 作者:行者123 更新时间:2023-12-04 15:57:54 26 4
gpt4 key购买 nike

我按照示例使用 Azure AD B2C 从 ASP.NET Web 应用程序调用 ASP.NET Web API:
https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi

我有一个关于 OpenIdConnectCachingSecurityTokenProvider 的问题

// This class is necessary because the OAuthBearer Middleware does not leverage
// the OpenID Connect metadata endpoint exposed by the STS by default.
public class OpenIdConnectCachingSecurityTokenProvider : IIssuerSecurityKeyProvider
{
public ConfigurationManager<OpenIdConnectConfiguration> _configManager;
private string _issuer;
private IEnumerable<SecurityKey> _keys;
private readonly string _metadataEndpoint;

private readonly ReaderWriterLockSlim _synclock = new ReaderWriterLockSlim();

public OpenIdConnectCachingSecurityTokenProvider(string metadataEndpoint)
{
_metadataEndpoint = metadataEndpoint;
_configManager = new ConfigurationManager<OpenIdConnectConfiguration>(metadataEndpoint, new OpenIdConnectConfigurationRetriever());

RetrieveMetadata();
}

/// <summary>
/// Gets the issuer the credentials are for.
/// </summary>
/// <value>
/// The issuer the credentials are for.
/// </value>
public string Issuer
{
get
{
RetrieveMetadata();
_synclock.EnterReadLock();
try
{
return _issuer;
}
finally
{
_synclock.ExitReadLock();
}
}
}

/// <summary>
/// Gets all known security keys.
/// </summary>
/// <value>
/// All known security keys.
/// </value>
public IEnumerable<SecurityKey> SecurityKeys
{
get
{
RetrieveMetadata();
_synclock.EnterReadLock();
try
{
return _keys;
}
finally
{
_synclock.ExitReadLock();
}
}
}

private void RetrieveMetadata()
{
_synclock.EnterWriteLock();
try
{
OpenIdConnectConfiguration config = Task.Run(_configManager.GetConfigurationAsync).Result;
_issuer = config.Issuer;
_keys = config.SigningKeys;
}
finally
{
_synclock.ExitWriteLock();
}
}
}

元数据端点:
https://login.microsoftonline.com/{TENANT}.onmicrosoft.com/v2.0/.well-known/openid-configuration?p={POLICY}

为什么我们总是需要打电话来检索 key 和发行人?

我可以缓存这些值吗?
如果是,到期的最佳设置是什么?

最佳答案

Why all the time we need to make a call to retrieve the keys and the issuer?


  • 签名 key :您的应用程序必须使用此签名 key (公钥)来验证由 AAD 使用其私钥签名的 token 。此元数据端点包含在特定时刻使用的所有公钥信息:
    https://login.microsoftonline.com/<yourtenantdomain>/discovery/v2.0/keys?p=<SigninPolicyName>
  • 发行人:您的应用程序需要发行人来验证 token 的 iss声称信任此 token 。也可以从 OpenID 连接元数据端点检索颁发者:
    https://login.microsoftonline.com/<YourTenantDomain>/v2.0/.well-known/openid-configuration?p=<SigninPolicyName>

  • Identifies the security token service (STS) that constructs and returns the token. In the tokens that Azure AD returns, the issuer is sts.windows.net. The GUID in the Issuer claim value is the tenant ID of the Azure AD directory. The tenant ID is an immutable and reliable identifier of the directory.



    另外, OAuthBearer Middleware默认情况下不利用此元数据端点,因此您需要使用代码检索它。 因此,您必须检索 key 和颁发者以验证 token 。

    Can I cache these values? If yes, what's the best setting for the expiration?



    是的,使用您发布的代码,它会将这些值缓存在 configManager.GetConfigurationAsync 中和 OpenIdConnectCachingSecurityTokenProvider启动时使用它。

    关于到期:签名 key 可以滚动。所以,不要担心唱歌键的设置过期。重要的是您最好动态获取元数据位置以保持签名 key 是正确的。

    引用:

    您可以在 this documentaion 中查看有关验证 B2C token 签名的详细信息。 .

    this documentation 中查看有关 AAD 中签名 key 翻转的更多详细信息.

    查看有关 OpendID 提供程序元数据的更多详细信息: http://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata

    关于azure-active-directory - 从元数据端点缓存颁发者和 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181242/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com