public static IDataProtectionBuilder ConfigureDataProtection(this IDataProtectionBuilder builder, DataProtectionOptions options)
        {
            builder.SetDefaultKeyLifetime(options.KeyLifeTime);
            builder.SetApplicationName(options.ApplicationName);
            var csBuilder = new System.Data.Common.DbConnectionStringBuilder
            {
                ConnectionString = options.ConnectionString
            };

            switch (options.Type)
            {
            case DataProtectionPersistenceType.FileSystem:
                var dirInfo = new DirectoryInfo(csBuilder["Path"].ToString());
                builder.PersistKeysToFileSystem(dirInfo);
                return(builder);

            case DataProtectionPersistenceType.Redis:
            {
                var uri      = csBuilder["uri"].ToString();
                var keystore = csBuilder["keystore"].ToString();
                if (string.IsNullOrWhiteSpace(keystore))
                {
                    keystore = "DataProtection-Keys";
                }
                var redis = ConnectionMultiplexer.Connect(uri);
                builder.PersistKeysToStackExchangeRedis(redis, keystore);
                return(builder);
            }

            default:
                throw new ArgumentOutOfRangeException($"No builder present for the specified type: [{options.Type}]");
            }
        }
    /// <summary>
    /// 使用Redis保存Keys
    /// <remark></remark>
    /// </summary>
    /// <param name="builder">当前对象</param>
    /// <param name="cachekey">redis key</param>
    /// <returns>返回IDataProtectionBuilder</returns>
    public static IDataProtectionBuilder PersistKeysToRedis(this IDataProtectionBuilder builder, string cachekey)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }
        builder.SetApplicationName(cachekey);
        var descriptor = ServiceDescriptor.Singleton <IXmlRepository> (services => new RedisXmlRepository(services.GetService <IDistributedCache> (), cachekey));

        builder.Services.Add(descriptor);
        return(builder.AddKeyManagementOptions(optinos => optinos.XmlRepository = builder.Services.BuildServiceProvider().GetService <IXmlRepository> ()));
    }
Esempio n. 3
0
        public static IDataProtectionBuilder EnableDistribution(this IDataProtectionBuilder builder)
        {
            builder.SetApplicationName("ZKEACMS");
            builder.Services.AddSingleton <IConfigureOptions <KeyManagementOptions> >(services =>
            {
                return(new ConfigureOptions <KeyManagementOptions>(options =>
                {
                    options.XmlRepository = new DistributionPersistKeysRepository(services);
                }));
            });

            return(builder);
        }