Browse Source

Added antiforgery options

pull/5728/head
Halil İbrahim Kalkan 6 years ago
parent
commit
71c97a0d89
  1. 5
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryCookieNameProvider.cs
  2. 29
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs
  3. 24
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAutoValidateAntiforgeryTokenAuthorizationFilter.cs
  4. 14
      framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs

5
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryCookieNameProvider.cs

@ -19,6 +19,11 @@ namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
public virtual string GetAuthCookieNameOrNull()
{
if (_abpAntiForgeryOptions.AuthCookieSchemaName == null)
{
return null;
}
return _namedOptionsAccessor.Get(_abpAntiForgeryOptions.AuthCookieSchemaName)?.Cookie?.Name;
}

29
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAntiForgeryOptions.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
@ -23,8 +24,34 @@ namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
/// </summary>
public bool AutoValidate { get; set; } = true;
/// <summary>
/// A predicate to filter types to auto-validate.
/// Return true to select the type to validate.
/// Default: returns true for all given types.
/// </summary>
[NotNull]
public Predicate<Type> AutoValidateFilter
{
get => _autoValidateFilter;
set => _autoValidateFilter = Check.NotNull(value, nameof(value));
}
private Predicate<Type> _autoValidateFilter;
/// <summary>
/// Default methods: "GET", "HEAD", "TRACE", "OPTIONS".
/// </summary>
[NotNull]
public HashSet<string> AutoValidateIgnoredHttpMethods
{
get => _autoValidateIgnoredHttpMethods;
set => _autoValidateIgnoredHttpMethods = Check.NotNull(value, nameof(value));
}
private HashSet<string> _autoValidateIgnoredHttpMethods;
public AbpAntiForgeryOptions()
{
AutoValidateFilter = type => true;
TokenCookie = new CookieBuilder
{
Name = "XSRF-TOKEN",
@ -34,6 +61,8 @@ namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
};
AuthCookieSchemaName = "Identity.Application";
AutoValidateIgnoredHttpMethods = new HashSet<string> {"GET", "HEAD", "TRACE", "OPTIONS"};
}
}
}

24
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AntiForgery/AbpAutoValidateAntiforgeryTokenAuthorizationFilter.cs

@ -1,5 +1,6 @@
using System;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -31,6 +32,19 @@ namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
return false;
}
if(context.ActionDescriptor.IsControllerAction())
{
var controllerType = context.ActionDescriptor
.AsControllerActionDescriptor()
.ControllerTypeInfo
.AsType();
if (!_options.AutoValidateFilter(controllerType))
{
return false;
}
}
if (IsIgnoredHttpMethod(context))
{
return false;
@ -41,11 +55,11 @@ namespace Volo.Abp.AspNetCore.Mvc.AntiForgery
protected virtual bool IsIgnoredHttpMethod(AuthorizationFilterContext context)
{
var method = context.HttpContext.Request.Method;
return string.Equals("GET", method, StringComparison.OrdinalIgnoreCase) ||
string.Equals("HEAD", method, StringComparison.OrdinalIgnoreCase) ||
string.Equals("TRACE", method, StringComparison.OrdinalIgnoreCase) ||
string.Equals("OPTIONS", method, StringComparison.OrdinalIgnoreCase);
return context.HttpContext
.Request
.Method
.ToUpperInvariant()
.IsIn(_options.AutoValidateIgnoredHttpMethods);
}
}
}

14
framework/src/Volo.Abp.Core/System/AbpObjectExtensions.cs

@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
@ -49,6 +50,17 @@ namespace System
return list.Contains(item);
}
/// <summary>
/// Check if an item is in the given enumerable.
/// </summary>
/// <param name="item">Item to check</param>
/// <param name="items">Items</param>
/// <typeparam name="T">Type of the items</typeparam>
public static bool IsIn<T>(this T item, IEnumerable<T> items)
{
return items.Contains(item);
}
/// <summary>
/// Can be used to conditionally perform a function
/// on an object and return the modified or the original object.

Loading…
Cancel
Save