Identity Server 4 AddOidcStateDataFormatterCacheTimeToLiveの構成

JavaadPatel

私は拡張機能を使用しています:

services.AddOidcStateDataFormatterCache();

Asp.Net Coreで、Redisを使用して実装された分散キャッシュに状態を保存するには:

services.AddStackExchangeRedisCache(options => {
                options.Configuration = Configuration[RedisConnection];
            });

しかし、RedisキャッシュのエントリはTT​​Lで設定されていないようです。 ここに画像の説明を入力してください

キャッシュに作成されるキーのTTLを制御する設定はありますか?

d_f

すでに報告されています。応答を待っています。(これも必要だと言ってください!)

今のところ、醜い継承者を使用します。ベースには仮想メソッドがなく、さらにヘルパーが必要なためinternal class ConfigureOpenIdConnectOptionsTTL : IPostConfigureOptions<OpenIdConnectOptions>(ほとんどの場合、再度コピーアンドペースト)、少なくとも「本番環境での遅いredis」は修正されました。

public class DistributedCacheStateDataFormatterTTL: 
    DistributedCacheStateDataFormatter, ISecureDataFormat<AuthenticationProperties>
{
    public static readonly TimeSpan DefaultCacheDuration = TimeSpan.FromMinutes(5);
    private readonly IHttpContextAccessor _httpContext;
    private readonly string _name;

    public DistributedCacheStateDataFormatterTTL(
        IHttpContextAccessor httpContext, string name) : base(httpContext, name)
    {
        _httpContext = httpContext;
        _name = name;
    }
    private string CacheKeyPrefix => "DistributedCacheStateDataFormatter";

    private IDistributedCache Cache =>
        _httpContext.HttpContext.RequestServices.GetRequiredService<IDistributedCache>();
    private IDataProtector Protector =>
       _httpContext.HttpContext.RequestServices
       .GetRequiredService<IDataProtectionProvider>()
       .CreateProtector(CacheKeyPrefix, _name);

    string ISecureDataFormat<AuthenticationProperties>
        .Protect(AuthenticationProperties data)
    {
         return ((ISecureDataFormat<AuthenticationProperties>)this).
             Protect(data, string.Empty);
    }


    string ISecureDataFormat<AuthenticationProperties>
        .Protect(AuthenticationProperties data, string purpose)
    {
         var key = Guid.NewGuid().ToString();
         var cacheKey = $"{CacheKeyPrefix}-{purpose}-{key}";
         var json = JsonConvert.SerializeObject(data, new JsonSerializerSettings()
         {
             DefaultValueHandling = DefaultValueHandling.Ignore,
             NullValueHandling = NullValueHandling.Ignore
         });

         var options = new DistributedCacheEntryOptions();
         if (data.ExpiresUtc.HasValue)
             options.SetAbsoluteExpiration(data.ExpiresUtc.Value);
         else
             options.SetSlidingExpiration(DefaultCacheDuration);

         // Rather than encrypt the full AuthenticationProperties
         // cache the data and encrypt the key that points to the data
         Cache.SetString(cacheKey, json, options);

         return Protector.Protect(key);
    }
}
internal class ConfigureOpenIdConnectOptionsTTL : IPostConfigureOptions<OpenIdConnectOptions>
{
    private string[] _schemes;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public ConfigureOpenIdConnectOptionsTTL(string[] schemes, IHttpContextAccessor httpContextAccessor)
    {
        _schemes = schemes ?? throw new ArgumentNullException(nameof(schemes));
        _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
    }

    public void PostConfigure(string name, OpenIdConnectOptions options)
    {
        // no schemes means configure them all
        if (_schemes.Length == 0 || _schemes.Contains(name))
        {
            options.StateDataFormat = new DistributedCacheStateDataFormatterTTL(_httpContextAccessor, name);
        }
    }


    public static IServiceCollection AddOidcStateDataFormatterCache(
        IServiceCollection services,
        params string[] schemes)
    {
        services.RemoveAll<IPostConfigureOptions<OpenIdConnectOptions>>();
        services.AddSingleton<IPostConfigureOptions<OpenIdConnectOptions>>(
            svcs => new ConfigureOpenIdConnectOptionsTTL(
                    schemes,
                    svcs.GetRequiredService<IHttpContextAccessor>())
            );

        return services;
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Identity Server 4 の認証

分類Dev

Identity Server 4

分類Dev

Identity Server 4 Federation Gateway

分類Dev

identity server 4 windows authentication

分類Dev

Identity Server 4 for NodeJS API

分類Dev

Identity Server4でnullのRefreshtoken

分類Dev

Identity Server 4Corsの問題

分類Dev

Identity Server 4 と Localhost 上の UWP

分類Dev

Identity Server4の構成および運用データコンテキストを拡張する

分類Dev

Identity Server 4:構成および運用データにEntityFrameworkCoreを使用する

分類Dev

In Identity Server 4, I have a User's identity but 0 Claims

分類Dev

Identity Server 4: adding claims to access token

分類Dev

Identity Server 4Swagger認証

分類Dev

Identity Server4とdocker

分類Dev

Is it possible to use a NoSQL for Identity Server 4?

分類Dev

Identity Server 4 Refresh Token Expiration Not Working

分類Dev

Identity Server4を使用するようにASP.NETMVC 4Webアプリを構成します

分類Dev

WSO2 Identity Server 5.9.0oAuth2タイプの構成

分類Dev

Identity Server4: 'http:// localhost:44338 / .well-known / openid-configurationから構成を取得できません

分類Dev

Identity Server4のAPIで空のクレーム

分類Dev

Identity Server 4 Angular2トークンの有効期限

分類Dev

Identity Server4のトラブルシューティング

分類Dev

Identity Server4セッション/ Cookie関連の質問

分類Dev

Identity Server4のカスタムログインビュー

分類Dev

Identity Server4の機能を拡張する

分類Dev

How to use Identity Server 4 Sign-in with desktop/mobile apps

分類Dev

Identity Server 4, API unauthorized to call introspection endpoint

分類Dev

Identity Server4ユーザー管理API

分類Dev

Identity Server 4 / nativescriptがハングする

Related 関連記事

  1. 1

    Identity Server 4 の認証

  2. 2

    Identity Server 4

  3. 3

    Identity Server 4 Federation Gateway

  4. 4

    identity server 4 windows authentication

  5. 5

    Identity Server 4 for NodeJS API

  6. 6

    Identity Server4でnullのRefreshtoken

  7. 7

    Identity Server 4Corsの問題

  8. 8

    Identity Server 4 と Localhost 上の UWP

  9. 9

    Identity Server4の構成および運用データコンテキストを拡張する

  10. 10

    Identity Server 4:構成および運用データにEntityFrameworkCoreを使用する

  11. 11

    In Identity Server 4, I have a User's identity but 0 Claims

  12. 12

    Identity Server 4: adding claims to access token

  13. 13

    Identity Server 4Swagger認証

  14. 14

    Identity Server4とdocker

  15. 15

    Is it possible to use a NoSQL for Identity Server 4?

  16. 16

    Identity Server 4 Refresh Token Expiration Not Working

  17. 17

    Identity Server4を使用するようにASP.NETMVC 4Webアプリを構成します

  18. 18

    WSO2 Identity Server 5.9.0oAuth2タイプの構成

  19. 19

    Identity Server4: 'http:// localhost:44338 / .well-known / openid-configurationから構成を取得できません

  20. 20

    Identity Server4のAPIで空のクレーム

  21. 21

    Identity Server 4 Angular2トークンの有効期限

  22. 22

    Identity Server4のトラブルシューティング

  23. 23

    Identity Server4セッション/ Cookie関連の質問

  24. 24

    Identity Server4のカスタムログインビュー

  25. 25

    Identity Server4の機能を拡張する

  26. 26

    How to use Identity Server 4 Sign-in with desktop/mobile apps

  27. 27

    Identity Server 4, API unauthorized to call introspection endpoint

  28. 28

    Identity Server4ユーザー管理API

  29. 29

    Identity Server 4 / nativescriptがハングする

ホットタグ

アーカイブ