Demonstration on lax and strict

Can anyone please provide

  1. A demonstration of how lax and strict (SameState) are implemented in Blazor application using Blazor web assembly and web app.
  2. How can we transfer and access cookie from one blazor site to another (hosted on different local host).

I tried to find quite a lot of tutorial and documentation on the same but could not find any.

In the WebAssembly App project add this code to add the cookie using the HttpClient in program.cs file:

using BlazorWebAssemblyApp1;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

var httpClientHandler = new HttpClientHandler();
httpClientHandler.CookieContainer = new System.Net.CookieContainer();

var httpClient = new HttpClient(httpClientHandler)
{
    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
};

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();

Code to fetch cookie:

@page "/fetch-cookies"
    @inject CookieService CookieService

<h3>Fetch Cookies</h3>

<button @onclick="FetchCookies">Fetch Cookies</button>

@if (!string.IsNullOrEmpty(LaxCookie))
{
    <p>Lax Cookie: @LaxCookie</p>
}

@if (!string.IsNullOrEmpty(StrictCookie))
{
    <p>Strict Cookie: @StrictCookie</p>
}

@code {
    private string LaxCookie;
    private string StrictCookie;

    private async Task FetchCookies()
    {
        LaxCookie = await CookieService.GetCookieAsync("SameSiteCookieLax");
        StrictCookie = await CookieService.GetCookieAsync("SameSiteCookieStrict");
    }
    }

Cookie.js :

window.blazorExtensions = {
       GetCookie: function (name) {
           let value = "; " + document.cookie;
           let parts = value.split("; " + name + "=");
           if (parts.length === 2) return parts.pop().split(";").shift();
       }
   };

CookieService.cs:(To get the cookie from browser)

using Microsoft.JSInterop;
    using System.Threading.Tasks;

    public class CookieService
    {
        private readonly IJSRuntime _jsRuntime;

        public CookieService(IJSRuntime jsRuntime)
        {
            _jsRuntime = jsRuntime;
        }

        public async Task<string> GetCookieAsync(string name)
        {
            return await _jsRuntime.InvokeAsync<string>("blazorExtensions.GetCookie", name);
        }
    }
    ```

    Add a JavaScript file (`wwwroot/cookies.js`) with the following content:

    ```javascript
    window.blazorExtensions = {
        GetCookie: function (name) {
            let value = "; " + document.cookie;
            let parts = value.split("; " + name + "=");
            if (parts.length === 2) return parts.pop().split(";").shift();
        }
    };

Do not forgot to add js file in index file:

In the sever size program.cs file set cors policy to configure CORS to allow the Blazor WebAssembly app to access it and set a cookie with a specific SameSite policy

using BlazorServerApp.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowBlazorClient",
        builder => builder
            .WithOrigins("https://localhost:7112") // URL of your Blazor WebAssembly app
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
});


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseCors("AllowBlazorClient");

app.Use(async (context, next) =>
{
    context.Response.Cookies.Append("SameSiteCookieLax", "lax_value", new CookieOptions { SameSite = SameSiteMode.Lax });
    context.Response.Cookies.Append("SameSiteCookieStrict", "strict_value", new CookieOptions { SameSite = SameSiteMode.Strict });
    await next.Invoke();
});


app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Demonstration on lax and strict

Can anyone please provide

  1. A demonstration of how lax and strict (SameState) are implemented in Blazor application using Blazor web assembly and web app.
  2. How can we transfer and access cookie from one blazor site to another (hosted on different local host).

I tried to find quite a lot of tutorial and documentation on the same but could not find any.

In the WebAssembly App project add this code to add the cookie using the HttpClient in program.cs file:

using BlazorWebAssemblyApp1;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

var httpClientHandler = new HttpClientHandler();
httpClientHandler.CookieContainer = new System.Net.CookieContainer();

var httpClient = new HttpClient(httpClientHandler)
{
    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
};

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();

Code to fetch cookie:

@page "/fetch-cookies"
    @inject CookieService CookieService

<h3>Fetch Cookies</h3>

<button @onclick="FetchCookies">Fetch Cookies</button>

@if (!string.IsNullOrEmpty(LaxCookie))
{
    <p>Lax Cookie: @LaxCookie</p>
}

@if (!string.IsNullOrEmpty(StrictCookie))
{
    <p>Strict Cookie: @StrictCookie</p>
}

@code {
    private string LaxCookie;
    private string StrictCookie;

    private async Task FetchCookies()
    {
        LaxCookie = await CookieService.GetCookieAsync("SameSiteCookieLax");
        StrictCookie = await CookieService.GetCookieAsync("SameSiteCookieStrict");
    }
    }

Cookie.js :

window.blazorExtensions = {
       GetCookie: function (name) {
           let value = "; " + document.cookie;
           let parts = value.split("; " + name + "=");
           if (parts.length === 2) return parts.pop().split(";").shift();
       }
   };

CookieService.cs:(To get the cookie from browser)

using Microsoft.JSInterop;
    using System.Threading.Tasks;

    public class CookieService
    {
        private readonly IJSRuntime _jsRuntime;

        public CookieService(IJSRuntime jsRuntime)
        {
            _jsRuntime = jsRuntime;
        }

        public async Task<string> GetCookieAsync(string name)
        {
            return await _jsRuntime.InvokeAsync<string>("blazorExtensions.GetCookie", name);
        }
    }
    ```

    Add a JavaScript file (`wwwroot/cookies.js`) with the following content:

    ```javascript
    window.blazorExtensions = {
        GetCookie: function (name) {
            let value = "; " + document.cookie;
            let parts = value.split("; " + name + "=");
            if (parts.length === 2) return parts.pop().split(";").shift();
        }
    };

Do not forgot to add js file in index file:

In the sever size program.cs file set cors policy to configure CORS to allow the Blazor WebAssembly app to access it and set a cookie with a specific SameSite policy

using BlazorServerApp.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowBlazorClient",
        builder => builder
            .WithOrigins("https://localhost:7112") // URL of your Blazor WebAssembly app
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
});


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseCors("AllowBlazorClient");

app.Use(async (context, next) =>
{
    context.Response.Cookies.Append("SameSiteCookieLax", "lax_value", new CookieOptions { SameSite = SameSiteMode.Lax });
    context.Response.Cookies.Append("SameSiteCookieStrict", "strict_value", new CookieOptions { SameSite = SameSiteMode.Strict });
    await next.Invoke();
});


app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật