mirror of https://github.com/abpframework/abp.git
6 changed files with 97 additions and 2 deletions
@ -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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
namespace Volo.Abp.AspNetCore.Mvc.AntiForgery |
||||
|
{ |
||||
|
public class AbpAntiForgeryOptions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Get/sets cookie name to transfer Anti Forgery token between server and client.
|
||||
|
/// Default value: "XSRF-TOKEN".
|
||||
|
/// </summary>
|
||||
|
public string TokenCookieName { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Get/sets header name to transfer Anti Forgery token from client to the server.
|
||||
|
/// Default value: "X-XSRF-TOKEN".
|
||||
|
/// </summary>
|
||||
|
public string TokenHeaderName { get; set; } |
||||
|
|
||||
|
public AbpAntiForgeryOptions() |
||||
|
{ |
||||
|
TokenCookieName = "XSRF-TOKEN"; |
||||
|
TokenHeaderName = "X-XSRF-TOKEN"; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<AbpAntiForgeryOptions> 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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue