From 5ee239c3c95dfe496f3b0577cf725c3e456b6336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lkay=20=C4=B0lknur?= Date: Fri, 16 Oct 2020 11:15:32 +0300 Subject: [PATCH] use a class to take optional parameters. --- .../Components/WebAssembly/CookieOptions.cs | 11 +++++++++++ .../Components/WebAssembly/CookieService.cs | 7 +++---- .../Components/WebAssembly/ICookieService.cs | 2 +- .../wwwroot/libs/abp/core/abp.js | 16 +++++++++++++++- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieOptions.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieOptions.cs new file mode 100644 index 0000000000..929995fa05 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieOptions.cs @@ -0,0 +1,11 @@ +using System; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public class CookieOptions + { + public DateTimeOffset? ExpireDate { get; set; } + public string Path { get; set; } + public bool Secure { get; set; } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs index 3d90d68ccd..c0dc94ebfa 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.JSInterop; using Volo.Abp.DependencyInjection; @@ -15,9 +14,9 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly JsRuntime = jsRuntime; } - public async ValueTask SetAsync(string key, string value, DateTimeOffset? expireDate = null, string path = null) + public async ValueTask SetAsync(string key, string value, CookieOptions options) { - await JsRuntime.InvokeVoidAsync("abp.utils.setCookieValue", key, value, expireDate?.ToString("r"), path); + await JsRuntime.InvokeVoidAsync("abp.utils.setCookieValue", key, value, options?.ExpireDate?.ToString("r"), options?.Path, options?.Secure); } public async ValueTask GetAsync(string key) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs index 63eebb6a5e..21254c9da5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs @@ -5,7 +5,7 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly { public interface ICookieService { - public ValueTask SetAsync(string key, string value, DateTimeOffset? expireDate = null, string path = null); + public ValueTask SetAsync(string key, string value, CookieOptions options = null); public ValueTask GetAsync(string key); public ValueTask DeleteAsync(string key, string path = null); } diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js index 4a04c50c84..1d16d0cc38 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js @@ -1,4 +1,14 @@ -window.abp.utils.setCookieValue = function (key, value, expireDate, path) { +/** + * Sets a cookie value for given key. + * This is a simple implementation created to be used by ABP. + * Please use a complete cookie library if you need. + * @param {string} key + * @param {string} value + * @param {string} expireDate (optional). If not specified the cookie will expire at the end of session. + * @param {string} path (optional) + * @param {bool} secure (optional) + */ +window.abp.utils.setCookieValue = function (key, value, expireDate, path, secure) { var cookieValue = encodeURIComponent(key) + '='; if (value) { cookieValue = cookieValue + encodeURIComponent(value); @@ -12,6 +22,10 @@ cookieValue = cookieValue + "; path=" + path; } + if (secure) { + cookieValue = cookieValue + "; secure"; + } + document.cookie = cookieValue; };