CookiePolicyOptions如何从数据库设置

时间:2019-09-03 11:58:08

标签: asp.net-core

我有存储库结构... IUnitOfWork unitOfWork,

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies 
            // is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

options.CheckConsentNeeded

我想从数据库中进行设置

var culture = Thread.CurrentThread.CurrentCulture.ToString();
var checkConsentNeededCookieettings = _unitOfWork.Settings.GetAll().Where(i => i.Language.Culture == culture).FirstOrDefault().CheckConsentNeededCookie;

您知道这些是服务

如何配置它以及它在哪里? ??中间件还是替代?

您能给我一些建议吗?和示例

3 个答案:

答案 0 :(得分:1)

为此,我将使用IPostConfigureOptions。创建一个实现IPostConfigureOptions<CookiePolicyOptions>的类:

internal class CookiePolicyPostConfigureOptions : IPostConfigureOptions<CookiePolicyOptions>
{
    private readonly IUnitOfWork _unitOfWork;

    public CookiePolicyPostConfigureOptions(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void PostConfigure(string name, CookiePolicyOptions options)
    {
        // do something with _unitOfWork and set values on `options`
    }
}

然后在Startup中输入

services.ConfigureOptions<CookiePolicyPostConfigureOptions>();

答案 1 :(得分:0)

如果我对CustomIdentityOptions使用相同的方式,则会出现错误:

InvalidOperationException:ValueFactory尝试访问此实例的Value属性。 System.Lazy.ViaFactory(LazyThreadSafetyMode模式)

IdentityOptions与CookiePolicyOptions之间有什么区别

services.ConfigureOptions<CustomIdentityOptions>();

internal class CustomIdentityOptions : IPostConfigureOptions<IdentityOptions>
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly ILogger<CustomIdentityOptions> _logger;

        public CustomIdentityOptions(IUnitOfWork unitOfWork, ILogger<CustomIdentityOptions> logger)
        {
            _unitOfWork = unitOfWork;
            _logger = logger;
        }

        public void PostConfigure(string name, IdentityOptions options)
        { 
            var staticSettings = _unitOfWork.StaticSettings.GetAll().OrderBy(i => i.StaticSettingId).FirstOrDefault();
            options.Lockout = new LockoutOptions
            {
                AllowedForNewUsers = staticSettings.AllowedForNewUsers,
                DefaultLockoutTimeSpan = TimeSpan.FromMinutes(staticSettings.DefaultLockoutTimeSpanFromMinutes),
                MaxFailedAccessAttempts = staticSettings.MaxFailedAccessAttempts
            };
            options.Password = new PasswordOptions
            {
                RequireDigit = staticSettings.RequireDigit,
                RequiredLength = staticSettings.RequiredLength,
                RequiredUniqueChars = staticSettings.RequiredUniqueChars,
                RequireLowercase = staticSettings.RequireLowercase,
                RequireNonAlphanumeric = staticSettings.RequireNonAlphanumeric,
                RequireUppercase = staticSettings.RequireUppercase
            };
            options.SignIn = new SignInOptions
            {
                RequireConfirmedEmail = staticSettings.RequireConfirmedEmail,
                RequireConfirmedPhoneNumber = staticSettings.RequireConfirmedPhoneNumber
            };
        }
    }

答案 2 :(得分:0)

我使用的是 .NET5,如果您的数据库/存储库是使用开箱即用的 DI 注入的,那么您可以直接从 httpContext 获取服务,因此只需使用如下所示的 lambda 函数:

 services.Configure<CookiePolicyOptions>( (options) => {
        options.CheckConsentNeeded = (context) => {            
            // get repo from DI
            var repo = context.RequestServices.GetService<IRepository>();
            // .. Determine result    
            return repo.GetAll().Where(i => i.Language.Culture == culture).FirstOrDefault().CheckConsentNeededCookie;            
        };
    });