1
0

- Fixed BasicAuthenticationAttribute.

- All attributes now reside in Microsoft.AspNetCore.Mvc.Filters namespace.
This commit is contained in:
2022-06-22 23:47:47 +02:00
parent 33c2b9336f
commit 97c3c303ce
5 changed files with 43 additions and 55 deletions

View File

@@ -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
{
/// <summary>
/// A basic authentication as attribute to use for specific actions.
/// </summary>
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
private readonly IServiceScopeFactory serviceScopeFactory;
/// <summary>
/// Initializes a new instance of the <see cref="BasicAuthenticationAttribute"/> class.
/// </summary>
/// <param name="serviceScopeFactory">A service scope factory.</param>
public BasicAuthenticationAttribute(IServiceScopeFactory serviceScopeFactory)
{
this.serviceScopeFactory = serviceScopeFactory;
}
/// <summary>
/// Gets or sets a username to validate.
/// </summary>
@@ -61,9 +48,7 @@ namespace AMWD.Common.AspNetCore.Attributes
return;
}
using var scope = serviceScopeFactory.CreateScope();
var logger = scope.ServiceProvider.GetService<ILogger<BasicAuthenticationAttribute>>();
var logger = context.HttpContext.RequestServices.GetService<ILogger<BasicAuthenticationAttribute>>();
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<IBasicAuthenticationValidator>();
var validator = context.HttpContext.RequestServices.GetService<IBasicAuthenticationValidator>();
var principal = await validator?.ValidateAsync(credentials.First(), credentials.Last(), context.HttpContext.GetRemoteIpAddress());
if (principal == null)
SetAuthenticateRequest(context);