diff --git a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs
index 337aefc..73110a6 100644
--- a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs
+++ b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs
@@ -5,29 +5,16 @@ using System.Text;
using System.Threading.Tasks;
using AMWD.Common.AspNetCore.BasicAuthentication;
using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
-namespace AMWD.Common.AspNetCore.Attributes
+namespace Microsoft.AspNetCore.Mvc.Filters
{
///
/// A basic authentication as attribute to use for specific actions.
///
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
- private readonly IServiceScopeFactory serviceScopeFactory;
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// A service scope factory.
- public BasicAuthenticationAttribute(IServiceScopeFactory serviceScopeFactory)
- {
- this.serviceScopeFactory = serviceScopeFactory;
- }
-
///
/// Gets or sets a username to validate.
///
@@ -61,9 +48,7 @@ namespace AMWD.Common.AspNetCore.Attributes
return;
}
- using var scope = serviceScopeFactory.CreateScope();
- var logger = scope.ServiceProvider.GetService>();
-
+ var logger = context.HttpContext.RequestServices.GetService>();
try
{
var authHeader = AuthenticationHeaderValue.Parse(context.HttpContext.Request.Headers["Authorization"]);
@@ -77,7 +62,7 @@ namespace AMWD.Common.AspNetCore.Attributes
return;
}
- var validator = scope.ServiceProvider.GetService();
+ var validator = context.HttpContext.RequestServices.GetService();
var principal = await validator?.ValidateAsync(credentials.First(), credentials.Last(), context.HttpContext.GetRemoteIpAddress());
if (principal == null)
SetAuthenticateRequest(context);
diff --git a/AMWD.Common.AspNetCore/Attributes/GoogleReCaptchaAttribute.cs b/AMWD.Common.AspNetCore/Attributes/GoogleReCaptchaAttribute.cs
index 82fcd6c..86cbde5 100644
--- a/AMWD.Common.AspNetCore/Attributes/GoogleReCaptchaAttribute.cs
+++ b/AMWD.Common.AspNetCore/Attributes/GoogleReCaptchaAttribute.cs
@@ -3,14 +3,34 @@ using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.Mvc.Filters
{
///
/// Custom filter attribute to use Google's reCaptcha (v3).
+ ///
/// Usage: [ServiceFilter(typeof(GoogleReCaptchaAttribute))]
///
+ ///
+ /// appsettings.json:
+ ///
+ ///
+ /// {
+ /// [...]
+ /// "Google": {
+ /// "ReCaptcha": {
+ /// "PrivateKey": "__private reCaptcha key__",
+ /// "PublicKey": "__public reCaptcha key__"
+ /// }
+ /// }
+ /// }
+ ///
+ ///
+ /// The score from google can be found on HttpContext.Items[GoogleReCaptchaAttribute.ScoreKey].
+ ///
+
public class GoogleReCaptchaAttribute : ActionFilterAttribute
{
///
@@ -30,33 +50,7 @@ namespace Microsoft.AspNetCore.Mvc.Filters
private const string VerificationUrl = "https://www.google.com/recaptcha/api/siteverify";
- private readonly string privateKey;
-
- ///
- /// Initializes a new instance of the class.
- ///
- ///
- /// appsettings.json:
- ///
- ///
- /// {
- /// [...]
- /// "Google": {
- /// "ReCaptcha": {
- /// "PrivateKey": "__private reCaptcha key__",
- /// "PublicKey": "__public reCaptcha key__"
- /// }
- /// }
- /// }
- ///
- ///
- /// The score from google can be found on HttpContext.Items[GoogleReCaptchaAttribute.ScoreKey].
- ///
- /// The application configuration.
- public GoogleReCaptchaAttribute(IConfiguration configuration)
- {
- privateKey = configuration.GetValue("Google:ReCaptcha:PrivateKey");
- }
+ private string privateKey;
///
/// Executes the validattion in background.
@@ -66,15 +60,18 @@ namespace Microsoft.AspNetCore.Mvc.Filters
/// An awaitable task.
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
+ var configuration = context.HttpContext.RequestServices.GetService();
+ privateKey = configuration?.GetValue("Google:ReCaptcha:PrivateKey");
+
+ if (string.IsNullOrWhiteSpace(privateKey))
+ return;
+
await DoValidation(context);
await base.OnActionExecutionAsync(context, next);
}
private async Task DoValidation(ActionExecutingContext context)
{
- if (string.IsNullOrWhiteSpace(privateKey))
- return;
-
if (!context.HttpContext.Request.HasFormContentType)
return;
diff --git a/AMWD.Common.AspNetCore/Attributes/IPBlacklistAttribute.cs b/AMWD.Common.AspNetCore/Attributes/IPBlacklistAttribute.cs
index c18dca5..9ca9c88 100644
--- a/AMWD.Common.AspNetCore/Attributes/IPBlacklistAttribute.cs
+++ b/AMWD.Common.AspNetCore/Attributes/IPBlacklistAttribute.cs
@@ -2,12 +2,10 @@
using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
-namespace AMWD.Common.AspNetCore.Attributes
+namespace Microsoft.AspNetCore.Mvc.Filters
{
///
/// Implements an IP filter. The defined addresses are blocked.
@@ -43,6 +41,7 @@ namespace AMWD.Common.AspNetCore.Attributes
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
+ context.HttpContext.Items["RemoteAddress"] = context.HttpContext.GetRemoteIpAddress();
if (!RestrictLocalAccess && context.HttpContext.IsLocalRequest())
return;
diff --git a/AMWD.Common.AspNetCore/Attributes/IPWhitelistAttribute.cs b/AMWD.Common.AspNetCore/Attributes/IPWhitelistAttribute.cs
index 27a7d48..ef2f9bb 100644
--- a/AMWD.Common.AspNetCore/Attributes/IPWhitelistAttribute.cs
+++ b/AMWD.Common.AspNetCore/Attributes/IPWhitelistAttribute.cs
@@ -2,12 +2,10 @@
using System.Net;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
-namespace AMWD.Common.AspNetCore.Attributes
+namespace Microsoft.AspNetCore.Mvc.Filters
{
///
/// Implements an IP filter. Only defined addresses are allowed to access.
@@ -43,6 +41,7 @@ namespace AMWD.Common.AspNetCore.Attributes
public override void OnActionExecuting(ActionExecutingContext context)
{
base.OnActionExecuting(context);
+ context.HttpContext.Items["RemoteAddress"] = context.HttpContext.GetRemoteIpAddress();
if (AllowLocalAccess && context.HttpContext.IsLocalRequest())
return;
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dcd479b..7c848fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,10 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
-## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.5.3...master) - 0000-00-00
+## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.6.0...master) - 0000-00-00
_nothing changed yet_
+## [v1.6.0](https://git.am-wd.de/AM.WD/common/compare/v1.5.3...v1.6.0) - 2022-06-22
+### Fixed
+- Fixed `BasicAuthenticationAttribute`.
+
+### Changed
+- All attributes now reside in `Microsoft.AspNetCore.Mvc.Filters` namespace.
+
+
## [v1.5.3](https://git.am-wd.de/AM.WD/common/compare/v1.5.2...v1.5.3) - 2022-06-22
### Fixed
- Fixed problem with `ForbidResult` without having an authentication schema defined.