Cookie
How to use the Cookie class of the bit Butil?
Usage
To use the browser cookie features you need to inject the Bit.Butil.Cookie class and use it like this:
@inject Bit.Butil.Cookie cookie @code { await cookie.Remove("cookie-name"); }
Methods
Set, Get:
Gets/Sets a cookie by providing the cookie name (MDN).
@inject Bit.Butil.Cookie cookie <BitTextField @bind-Value="newCookieName" Label="Cookie name" /> <BitTextField @bind-Value="newCookieValue" Label="Cookie value" /> <BitButton OnClick="@SetCookie">Set</BitButton> <BitTextField @bind-Value="getCookieName" Label="Cookie name" /> <BitButton OnClick="@GetCookie">Get</BitButton> <div>Cookie value: @getCookieValue</div> @code { private string? newCookieName; private string? newCookieValue; private string? getCookieName; private string? getCookieValue; private async Task SetCookie() { await cookie.Set(new ButilCookie { Name = newCookieName, Value = newCookieValue }); } private async Task GetCookie() { var result = await cookie.Get(getCookieName!); currentCookieValue = result?.Value; } }
GetAll:
Gets all cookies registered on the current document.
@inject Bit.Butil.Cookie cookie <BitButton OnClick="@GetAllCookies">GetAll</BitButton> <div>Cookies: @getAllCookieValues</div> @code { private string? getAllCookieValues; private async Task GetAllCookies() { getAllCookieValues = string.Join<ButilCookie>(", ", await cookie.GetAll()); } }
GetValue:
Returns the cookie value by providing its name.
@inject Bit.Butil.Cookie cookie <BitTextField @bind-Value="getValueCookieName" Label="Cookie name" Style="max-width: 18.75rem;" /> <BitButton OnClick="@GetValue">GetValue</BitButton> <div>Cookie value: @getValueCookieValue</div> @code { private string? getValueCookieName; private string? getValueCookieValue; private async Task GetValue() { getValueCookieValue = await cookie.GetValue(getValueCookieName!); } }
Remove:
Removes a cookie by providing the its name.
@inject Bit.Butil.Cookie cookie <BitTextField @bind-Value="removeCookieName" Label="Cookie name" /> <BitButton OnClick="@RemoveCookie">Remove</BitButton> @code { private string? removeCookieName; private async Task RemoveCookie() { await cookie.Remove(removeCookieName!); } }