最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get language cookies in ASP.NET Core 8.0 Blazor server app? - Stack Overflow

programmeradmin4浏览0评论

I can't get the language cookies in Blazor Server App. I have client and server on the same host, but cookies are not sent with the request. And I get the default language here, although cookies are set correctly in the browser and the interface is localized normally:

[HttpGet("destinations/getPaginated/{companyId}/{page}/{pageSize}")]
public async Task<IActionResult> GetPaginatedDestinationsByCompanyId(long companyId, int page, int pageSize)
{
    try
    {
        var requestCultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
        var language = requestCultureFeature?.RequestCulture?.Culture?.TwoLetterISOLanguageName;

        return Ok(await _destinationsQueries.GetPaginatedDestinationsByCompanyId(page, pageSize, language, companyId));
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }
}

Localization works for me as described in Microsoft documentation. But I can't get the installed language when running in the endpoint, I don't get any cookies from the browser. I tried changing the cookie settings, but without result

My Program.cs:

var supportedCultures = builder.Configuration
    .GetSection("SupportedCultures")
    .Get<string[]>();

var localizationOptions = new RequestLocalizationOptions
    {
        ApplyCurrentCultureToResponseHeaders = true
    }
    .SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);

localizationOptions.RequestCultureProviders.Insert(0, new CookieRequestCultureProvider());
app.UseHttpsRedirection();

app.UseStaticFiles();
app.UseRouting();
app.UseRequestLocalization(localizationOptions);
app.MapControllers();
app.UseAntifery();

app.UseAuthentication();
app.UseAuthorization();

My culture set endpoint:

[Route("[controller]/[action]")]
public class CultureController : Controller
{
    [AllowAnonymous]
    [ApiExplorerSettings(IgnoreApi = true)]
    public IActionResult Set(string culture, string redirectUri)
    {
        if (culture != null)
        {
            HttpContext.Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(
                    new RequestCulture(culture, culture)),
                new CookieOptions
                {
                    Path = "/",
                    SameSite = SameSiteMode.None, 
                    Secure = false,               
                    HttpOnly = false
                });
        }

        return LocalRedirect(redirectUri);
    }
}

UPDATE

I noticed that in Swagger (https://localhost:7048/swagger/index.html) everything works correctly, but when I make a request from the client (https://localhost:7048/), I can no longer get the current language from cookies

发布评论

评论列表(0)

  1. 暂无评论