Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

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

$
0
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.UseAntiforgery();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


Viewing all articles
Browse latest Browse all 4839

Trending Articles