Ejemplo n.º 1
0
        public void DeleteCookie(string name, string?domain = null, string?path = null)
        {
            var setCookieRequest = new SetCookieRequest(new Cookie(name, string.Empty))
            {
                Expires = DateTimeOffset.MinValue,
                Path    = path,
                Domain  = domain
            };

            SetCookie(setCookieRequest);
        }
Ejemplo n.º 2
0
        public void SetCookie(SetCookieRequest request)
        {
            var httpCookie = new HttpCookie(request.Cookie.Name, request.Domain ?? string.Empty, request.Path ?? string.Empty)
            {
                Secure  = request.Secure,
                Expires = request.Expires,
                Value   = request.Cookie.Value,
            };
            var serializedCookie = httpCookie.ToString();

            if (request.MaxAge != null)
            {
                serializedCookie += $"; max-age={request.MaxAge.Value.ToString(CultureInfo.InvariantCulture)}";
            }
            if (request.SameSite != null)
            {
                serializedCookie += $"; samesite={request.SameSite.Value.ToString("g").ToLowerInvariant()}";
            }

            var escapedCookie = WebAssemblyRuntime.EscapeJs(serializedCookie);
            var jsInvoke      = $"document.cookie = '{escapedCookie}'";

            WebAssemblyRuntime.InvokeJS(jsInvoke);
        }