简体   繁体   English

CookiePolicyOptions 干扰 Blazor 服务器端应用程序中的本地化 cookie

[英]CookiePolicyOptions interfering with localization cookie in Blazor server-side app

I have a Blazor server-side and I have the following cookie policy options set up:我有一个 Blazor 服务器端,并设置了以下 cookie 策略选项:

services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

The app also implements cookie localization as described in this MS article .该应用程序还实现了本MS 文章中所述的 cookie 本地化。

However, the problem is that with the above policy options, the localization cookie is not being created.但是,问题是使用上述策略选项时,不会创建本地化 cookie。

If I change the policy options to (or remove them completely):如果我将策略选项更改为(或完全删除它们):

services.Configure<CookiePolicyOptions>(options =>
{
    options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
});

The localization cookie is created and localization works as expected.本地化 cookie 已创建,本地化按预期工作。 So the original options are interfering with the localization cookie.因此,原始选项干扰了本地化 cookie。

A sample Blazor project experiencing this problem is found in this GitHub repo .在此GitHub 存储库中找到了遇到此问题的示例 Blazor 项目。

Is there a solution/workaround in which I have the original cookie policy options (required for consent) and also a working localization cookie?是否有解决方案/解决方法,其中我有原始 cookie 策略选项(需要同意)以及有效的本地化 cookie?

Two changes must be done do get it working:必须进行两项更改才能使其正常工作:

Change the cooking policy options as follows:更改烹饪策略选项如下:

services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
    options.Secure = CookieSecurePolicy.Always;
});

Change the code where the culture is set as follows:更改设置文化的代码如下:

[Route("[controller]/[action]")]
public class CultureController : Controller
{
    public IActionResult Set(string culture, string redirectUri)
    {
        if (culture != null)
        {
            HttpContext.Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(
                    new RequestCulture(culture, culture)), 
                    new CookieOptions {
                        Expires = DateTimeOffset.UtcNow.AddYears(1),
                        IsEssential = true,
                        Path = "/",
                        HttpOnly = false,
                    }
            );
        }

        return LocalRedirect(redirectUri);
    }
}

IsEssential is the most important property as it tells the cookie to bypass any cookie policy. IsEssential是最重要的属性,因为它告诉 cookie 绕过任何 cookie 策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM