In Blazor, you can create a cookie client-side using the jsRuntime service to call JavaScript code that creates and sets the cookie. Here's an example:
IJSRuntime service into your Blazor component:@inject IJSRuntime jsRuntime
private async Task SetCookieAsync(string name, string value, int expires) { await jsRuntime.InvokeVoidAsync("setCookie", name, value, expires); } In this example, the SetCookieAsync method takes three parameters:
name: The name of the cookie.value: The value of the cookie.expires: The number of days until the cookie expires.The method uses the jsRuntime.InvokeVoidAsync method to call a JavaScript function named setCookie, passing in the name, value, and expires parameters.
setCookie JavaScript function in a script tag in your component's Razor file:<script> function setCookie(name, value, expires) { var date = new Date(); date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); var expiresStr = "expires=" + date.toUTCString(); document.cookie = name + "=" + value + ";" + expiresStr + ";path=/"; } </script> In this example, the setCookie function takes three parameters:
name: The name of the cookie.value: The value of the cookie.expires: The number of days until the cookie expires.The function creates a Date object representing the expiration date of the cookie, and sets the cookie's expiration date using the expires parameter. It then sets the document.cookie property to a string containing the cookie name, value, and expiration date.
You can then call the SetCookieAsync method from your component's code to create and set a cookie. For example:
await SetCookieAsync("myCookie", "myValue", 7); This will create a cookie named "myCookie" with a value of "myValue" that expires in 7 days.
"Blazor set cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetCookieAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourCookieName=yourCookieValue; expires=Thu, 18 Dec 2023 12:00:00 UTC; path=/;"); } } "Blazor read cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task<string> ReadCookieAsync() { return await JSRuntime.InvokeAsync<string>("document.cookie"); } } "Blazor delete cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task DeleteCookieAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourCookieName=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"); } } "Blazor create persistent cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetPersistentCookieAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourPersistentCookieName=yourCookieValue; max-age=31536000; path=/;"); } } "Blazor check if cookie exists client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task<bool> DoesCookieExistAsync(string cookieName) { var cookies = await JSRuntime.InvokeAsync<string>("document.cookie"); return cookies.Contains(cookieName); } } "Blazor update cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task UpdateCookieAsync(string updatedValue) { await JSRuntime.InvokeVoidAsync("document.cookie", "yourCookieName=" + updatedValue + "; path=/;"); } } "Blazor secure cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetSecureCookieAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourSecureCookieName=yourSecureCookieValue; Secure; path=/;"); } } "Blazor HttpOnly cookie client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetHttpOnlyCookieAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourHttpOnlyCookieName=yourCookieValue; HttpOnly; path=/;"); } } "Blazor cookie domain and path client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetCookieWithDomainAndPathAsync() { await JSRuntime.InvokeVoidAsync("document.cookie", "yourCookieName=yourCookieValue; domain=yourdomain.com; path=/yourpath;"); } } "Blazor cookie expiration client-side"
@code { [Inject] IJSRuntime JSRuntime { get; set; } async Task SetCookieWithExpirationAsync(DateTime expirationDate) { var formattedDate = expirationDate.ToUniversalTime().ToString("R"); await JSRuntime.InvokeVoidAsync("document.cookie", $"yourCookieName=yourCookieValue; expires={formattedDate}; path=/;"); } } return-code touchpad jquery-chosen python-unittest.mock pycrypto android-sdcard android-tv laravel-artisan tedious simplewebrtc