From 40d8bb91dc3762c9f6e3f39354d232f1f93139b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lkay=20=C4=B0lknur?= Date: Wed, 14 Oct 2020 16:57:39 +0300 Subject: [PATCH 1/4] ICookieService implementation. --- .../Components/WebAssembly/CookieService.cs | 33 +++++++++++++++++++ .../Components/WebAssembly/ICookieService.cs | 12 +++++++ 2 files changed, 45 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs 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 new file mode 100644 index 0000000000..3d90d68ccd --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/CookieService.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading.Tasks; +using Microsoft.JSInterop; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + [Dependency(ReplaceServices = true)] + public class CookieService : ICookieService, ITransientDependency + { + public IJSRuntime JsRuntime { get; } + + public CookieService(IJSRuntime jsRuntime) + { + JsRuntime = jsRuntime; + } + + public async ValueTask SetAsync(string key, string value, DateTimeOffset? expireDate = null, string path = null) + { + await JsRuntime.InvokeVoidAsync("abp.utils.setCookieValue", key, value, expireDate?.ToString("r"), path); + } + + public async ValueTask GetAsync(string key) + { + return await JsRuntime.InvokeAsync("abp.utils.getCookieValue", key); + } + + public async ValueTask DeleteAsync(string key, string path = null) + { + await JsRuntime.InvokeVoidAsync("abp.utils.deleteCookie", key); + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..63eebb6a5e --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ICookieService.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public interface ICookieService + { + public ValueTask SetAsync(string key, string value, DateTimeOffset? expireDate = null, string path = null); + public ValueTask GetAsync(string key); + public ValueTask DeleteAsync(string key, string path = null); + } +} \ No newline at end of file From e01e79775973aee7d99206e30d237bce7dff28aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lkay=20=C4=B0lknur?= Date: Wed, 14 Oct 2020 16:57:56 +0300 Subject: [PATCH 2/4] abp.js added to the template. --- .../wwwroot/index.html | 1 + .../wwwroot/libs/abp/core/abp.js | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html index 4566bec3c1..b2b716caa2 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html @@ -29,6 +29,7 @@ + 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 new file mode 100644 index 0000000000..4a04c50c84 --- /dev/null +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js @@ -0,0 +1,62 @@ +window.abp.utils.setCookieValue = function (key, value, expireDate, path) { + var cookieValue = encodeURIComponent(key) + '='; + if (value) { + cookieValue = cookieValue + encodeURIComponent(value); + } + + if (expireDate) { + cookieValue = cookieValue + "; expires=" + expireDate; + } + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + document.cookie = cookieValue; +}; + +/** + * Gets a cookie with 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 + * @returns {string} Cookie value or null + */ +window.abp.utils.getCookieValue = function (key) { + var equalities = document.cookie.split('; '); + for (var i = 0; i < equalities.length; i++) { + if (!equalities[i]) { + continue; + } + + var splitted = equalities[i].split('='); + if (splitted.length != 2) { + continue; + } + + if (decodeURIComponent(splitted[0]) === key) { + return decodeURIComponent(splitted[1] || ''); + } + } + + return null; +}; + +/** + * Deletes cookie 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} path (optional) + */ +window.abp.utils.deleteCookie = function (key, path) { + var cookieValue = encodeURIComponent(key) + '='; + + cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + document.cookie = cookieValue; +} \ No newline at end of file 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 3/4] 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; }; From d4c41873c7558aec5360adae22ce7648c396249c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lkay=20=C4=B0lknur?= Date: Mon, 19 Oct 2020 11:40:46 +0300 Subject: [PATCH 4/4] embed abp.js to the webcomponents assembly. --- ...p.AspNetCore.Components.WebAssembly.csproj | 2 +- .../wwwroot/abp.js | 80 +++++++++++++++++++ .../wwwroot/index.html | 2 +- .../wwwroot/libs/abp/core/abp.js | 76 ------------------ 4 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/wwwroot/abp.js delete mode 100644 templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj index 44a816678c..b7aa91c74e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj @@ -1,4 +1,4 @@ - + diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/wwwroot/abp.js b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/wwwroot/abp.js new file mode 100644 index 0000000000..b945daa56f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/wwwroot/abp.js @@ -0,0 +1,80 @@ +var abp = abp || {}; +(function () { + abp.utils = abp.utils || {}; + /** + * 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) + */ + abp.utils.setCookieValue = function (key, value, expireDate, path, secure) { + var cookieValue = encodeURIComponent(key) + '='; + if (value) { + cookieValue = cookieValue + encodeURIComponent(value); + } + + if (expireDate) { + cookieValue = cookieValue + "; expires=" + expireDate; + } + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + if (secure) { + cookieValue = cookieValue + "; secure"; + } + + document.cookie = cookieValue; + }; + + /** + * Gets a cookie with 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 + * @returns {string} Cookie value or null + */ + abp.utils.getCookieValue = function (key) { + var equalities = document.cookie.split('; '); + for (var i = 0; i < equalities.length; i++) { + if (!equalities[i]) { + continue; + } + + var splitted = equalities[i].split('='); + if (splitted.length != 2) { + continue; + } + + if (decodeURIComponent(splitted[0]) === key) { + return decodeURIComponent(splitted[1] || ''); + } + } + + return null; + }; + + /** + * Deletes cookie 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} path (optional) + */ + abp.utils.deleteCookie = function (key, path) { + var cookieValue = encodeURIComponent(key) + '='; + + cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); + + if (path) { + cookieValue = cookieValue + "; path=" + path; + } + + document.cookie = cookieValue; + } +})(); \ No newline at end of file diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html index b2b716caa2..5bba3d3350 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html @@ -29,7 +29,7 @@ - + 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 deleted file mode 100644 index 1d16d0cc38..0000000000 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/libs/abp/core/abp.js +++ /dev/null @@ -1,76 +0,0 @@ -/** - * 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); - } - - if (expireDate) { - cookieValue = cookieValue + "; expires=" + expireDate; - } - - if (path) { - cookieValue = cookieValue + "; path=" + path; - } - - if (secure) { - cookieValue = cookieValue + "; secure"; - } - - document.cookie = cookieValue; -}; - -/** - * Gets a cookie with 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 - * @returns {string} Cookie value or null - */ -window.abp.utils.getCookieValue = function (key) { - var equalities = document.cookie.split('; '); - for (var i = 0; i < equalities.length; i++) { - if (!equalities[i]) { - continue; - } - - var splitted = equalities[i].split('='); - if (splitted.length != 2) { - continue; - } - - if (decodeURIComponent(splitted[0]) === key) { - return decodeURIComponent(splitted[1] || ''); - } - } - - return null; -}; - -/** - * Deletes cookie 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} path (optional) - */ -window.abp.utils.deleteCookie = function (key, path) { - var cookieValue = encodeURIComponent(key) + '='; - - cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString(); - - if (path) { - cookieValue = cookieValue + "; path=" + path; - } - - document.cookie = cookieValue; -} \ No newline at end of file