From 360be9724a4fdf4f47dfdeb1c9699072e37d0ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 1 May 2018 08:59:31 +0300 Subject: [PATCH] Created IAbpAntiForgeryManager --- .../Button/AbpButtonTagHelperService.cs | 9 ++++- .../Views/Shared/_AppLayout.cshtml | 5 ++- ...pAntiForgeryManagerAspNetCoreExtensions.cs | 10 +++++ .../Mvc/AntiForgery/AbpAntiForgeryOptions.cs | 23 ++++++++++++ .../AspNetCoreAbpAntiForgeryManager.cs | 37 +++++++++++++++++++ .../Mvc/AntiForgery/IAbpAntiForgeryManager.cs | 15 ++++++++ 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryManagerAspNetCoreExtensions.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AspNetCoreAbpAntiForgeryManager.cs create mode 100644 src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/IAbpAntiForgeryManager.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Button/AbpButtonTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Button/AbpButtonTagHelperService.cs index 99148802b0..d56a96a8c7 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Button/AbpButtonTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Button/AbpButtonTagHelperService.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Microsoft.AspNetCore.Razor.TagHelpers; namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button @@ -15,6 +16,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Button { output.Attributes.AddClass("btn-" + TagHelper.ButtonType.ToString().ToLowerInvariant()); } + + if (string.Equals(output.Attributes["type"]?.Value.ToString(), "submit", StringComparison.OrdinalIgnoreCase) && + !output.Attributes.ContainsName("data-busy-text")) + { + output.Attributes.SetAttribute("data-busy-text", "Processing..."); + } } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/_AppLayout.cshtml b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/_AppLayout.cshtml index c212bce6e9..26383fe179 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/_AppLayout.cshtml +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic/Views/Shared/_AppLayout.cshtml @@ -1,6 +1,9 @@ -@using Volo.Abp.UI.Navigation +@using Volo.Abp.AspNetCore.Mvc.AntiForgery +@using Volo.Abp.UI.Navigation +@inject IAbpAntiForgeryManager AbpAntiForgeryManager @{ Layout = null; + AbpAntiForgeryManager.SetCookie(); } diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryManagerAspNetCoreExtensions.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryManagerAspNetCoreExtensions.cs new file mode 100644 index 0000000000..c5c992b364 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryManagerAspNetCoreExtensions.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.AspNetCore.Mvc.AntiForgery +{ + public static class AbpAntiForgeryManagerAspNetCoreExtensions + { + public static void SetCookie(this IAbpAntiForgeryManager manager) + { + manager.HttpContext.Response.Cookies.Append(manager.Options.TokenCookieName, manager.GenerateToken()); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs new file mode 100644 index 0000000000..0dc573fd5d --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs @@ -0,0 +1,23 @@ +namespace Volo.Abp.AspNetCore.Mvc.AntiForgery +{ + public class AbpAntiForgeryOptions + { + /// + /// Get/sets cookie name to transfer Anti Forgery token between server and client. + /// Default value: "XSRF-TOKEN". + /// + public string TokenCookieName { get; set; } + + /// + /// Get/sets header name to transfer Anti Forgery token from client to the server. + /// Default value: "X-XSRF-TOKEN". + /// + public string TokenHeaderName { get; set; } + + public AbpAntiForgeryOptions() + { + TokenCookieName = "XSRF-TOKEN"; + TokenHeaderName = "X-XSRF-TOKEN"; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AspNetCoreAbpAntiForgeryManager.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AspNetCoreAbpAntiForgeryManager.cs new file mode 100644 index 0000000000..f016c36671 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AspNetCoreAbpAntiForgeryManager.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Antiforgery; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Mvc.AntiForgery +{ + public class AspNetCoreAbpAntiForgeryManager : IAbpAntiForgeryManager, ITransientDependency + { + public AbpAntiForgeryOptions Options { get; } + + public HttpContext HttpContext => _httpContextAccessor.HttpContext; + + private readonly IAntiforgery _antiforgery; + private readonly IHttpContextAccessor _httpContextAccessor; + + public AspNetCoreAbpAntiForgeryManager( + IAntiforgery antiforgery, + IHttpContextAccessor httpContextAccessor, + IOptions options) + { + _antiforgery = antiforgery; + _httpContextAccessor = httpContextAccessor; + Options = options.Value; + } + + public void SetCookie() + { + HttpContext.Response.Cookies.Append(Options.TokenCookieName, GenerateToken()); + } + + public string GenerateToken() + { + return _antiforgery.GetAndStoreTokens(_httpContextAccessor.HttpContext).RequestToken; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/IAbpAntiForgeryManager.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/IAbpAntiForgeryManager.cs new file mode 100644 index 0000000000..359e857dcb --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/IAbpAntiForgeryManager.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Http; + +namespace Volo.Abp.AspNetCore.Mvc.AntiForgery +{ + public interface IAbpAntiForgeryManager + { + AbpAntiForgeryOptions Options { get; } + + HttpContext HttpContext { get; } + + void SetCookie(); + + string GenerateToken(); + } +}