【问题标题】:Caching issuer and keys from the metadata endpoint从元数据端点缓存颁发者和密钥
【发布时间】:2018-07-06 09:43:20
【问题描述】:

我遵循了使用 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}

为什么我们总是需要调用来检索密钥和颁发者?

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

【问题讨论】:

  • 您添加的类将从 Startup 调用,它会在其生命周期内调用一次。所以您不必担心 元数据端点的缓存。

标签: azure-active-directory identity azure-ad-b2c


【解决方案1】:

为什么我们总是需要调用来检索密钥和 发行人?

  • 签名密钥:您的应用必须使用此签名密钥(公钥)来验证 AAD 使用其私钥签名的令牌。此元数据端点包含特定时刻正在使用的所有公钥信息:

    https://login.microsoftonline.com/&lt;yourtenantdomain&gt;/discovery/v2.0/keys?p=&lt;SigninPolicyName&gt;

  • Issuer :您的应用程序需要 Issuer 来验证令牌的 iss 声明以信任此令牌。还可以从 OpenID 连接元数据端点检索颁发者:

    https://login.microsoftonline.com/&lt;YourTenantDomain&gt;/v2.0/.well-known/openid-configuration?p=&lt;SigninPolicyName&gt;

标识构建和构建的安全令牌服务 (STS) 返回令牌。在 Azure AD 返回的令牌中,颁发者是 sts.windows.net。 Issuer 声明值中的 GUID 是租户 ID Azure AD 目录。租户 ID 是不可变且可靠的 目录的标识符。

此外,OAuthBearer Middleware 默认情况下不利用此元数据端点,因此您需要使用代码检索它。 因此,您必须检索密钥和颁发者才能验证令牌。

我可以缓存这些值吗?如果是,什么是最适合的设置 过期了?

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

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

参考:

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

this documentation 中查看有关在 AAD 中签名密钥翻转的更多详细信息。

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

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2019-02-13
    • 2016-05-23
    • 2020-08-12
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多