Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

blazor clientside cookies

using Microsoft.JSInterop;

namespace MyProject.Utils
{
    public interface ICookie
    {
        public Task SetValue(string key, string value, int? days = null);
        public Task<string> GetValue(string key, string def = "");
    }

    public class Cookie : ICookie
    {
        readonly IJSRuntime JSRuntime;
        string expires = "";

        public Cookie(IJSRuntime jsRuntime)
        {
            JSRuntime = jsRuntime;
            ExpireDays = 300;
        }

        public async Task SetValue(string key, string value, int? days = null)
        {
            var curExp = (days != null) ? (days > 0 ? DateToUTC(days.Value) : "") : expires;
            await SetCookie($"{key}={value}; expires={curExp}; path=/");
        }

        public async Task<string> GetValue(string key, string def = "")
        {
            var cValue = await GetCookie();
            if (string.IsNullOrEmpty(cValue)) return def;                

            var vals = cValue.Split(';');
            foreach (var val in vals)
                if(!string.IsNullOrEmpty(val) && val.IndexOf('=') > 0)
                    if(val.Substring(1, val.IndexOf('=') - 1).Trim().Equals(key, StringComparison.OrdinalIgnoreCase))
                        return val.Substring(val.IndexOf('=') + 1);
            return def;
        }

        private async Task SetCookie(string value)
        {
            await JSRuntime.InvokeVoidAsync("eval", $"document.cookie = "{value}"");
        }

        private async Task<string> GetCookie()
        {
            return await JSRuntime.InvokeAsync<string>("eval", $"document.cookie");
        }

        public int ExpireDays
        {
            set => expires = DateToUTC(value);
        }

        private static string DateToUTC(int days) => DateTime.Now.AddDays(days).ToUniversalTime().ToString("R");
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# internalsvisibleto 
Csharp :: how to print to printer in c# 
Csharp :: how to make a character jump c# 
Csharp :: Merge two List using Linq 
Csharp :: only specific columns in Linq 
Csharp :: C# check if object is default 
Csharp :: demand a Security action c# 
Csharp :: print hello world in unity 
Csharp :: dictionaries in unity 
Csharp :: C# http post request with file 
Csharp :: find gameobject by name in root 
Csharp :: c# double without exponential notation 
Csharp :: c# string to binary 
Csharp :: mvc model validation for decimal type 
Csharp :: query parameters sending to controller action asp.net core 
Csharp :: do while loop in c# 
Csharp :: get gameobject layermask 
Csharp :: how to check if button is pressed unity 
Csharp :: c# async and await example 
Csharp :: How to set default page asp.net MVC 
Csharp :: how to jump in unity using physics 
Csharp :: c# get all id of list object 
Csharp :: delete items in c# 
Csharp :: delete selected cells in Datagridview 
Csharp :: exception is null c# 
Csharp :: unity set cursor position 
Csharp :: Storing Data within your TileEntity 
Csharp :: DataGridView set column cell Combobox 
Csharp :: c# predicate 
Csharp :: c# ping all machines on local network 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =