Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

AuthenticationMiddleware.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Nop.Core.Domain.Customers;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Services.Logging;

namespace Nop.Services.Authentication;

/// 
/// Represents middleware that enables authentication
/// 
public partial class AuthenticationMiddleware
{
    #region Fields

    protected readonly RequestDelegate _next;

    #endregion

    #region Ctor

    public AuthenticationMiddleware(IAuthenticationSchemeProvider schemes, RequestDelegate next)
    {
        ArgumentNullException.ThrowIfNull(schemes);
        Schemes = schemes;

        ArgumentNullException.ThrowIfNull(next);
        _next = next;
    }

    #endregion

    #region Methods

    /// 
    /// Invoke middleware actions
    /// 
    /// HTTP context
    /// A task that represents the asynchronous operation
    public async Task InvokeAsync(HttpContext context)
    {
        context.Features.Set(new AuthenticationFeature
        {
            OriginalPath = context.Request.Path,
            OriginalPathBase = context.Request.PathBase
        });

        // Give any IAuthenticationRequestHandler schemes a chance to handle the request
        var handlers = EngineContext.Current.Resolve();
        foreach (var scheme in await Schemes.GetRequestHandlerSchemesAsync())
        {
            try
            {
                if (await handlers.GetHandlerAsync(context, scheme.Name) is IAuthenticationRequestHandler handler && await handler.HandleRequestAsync())
                    return;
            }
            catch (Exception ex)
            {
                if (!DataSettingsManager.IsDatabaseInstalled())
                    continue;

                var externalAuthenticationSettings =
                    EngineContext.Current.Resolve();

                if (!externalAuthenticationSettings.LogErrors)
                    continue;

                var logger =
                    EngineContext.Current.Resolve();

                await logger.ErrorAsync(ex.Message, ex);
            }
        }

        var defaultAuthenticate = await Schemes.GetDefaultAuthenticateSchemeAsync();
        if (defaultAuthenticate != null)
        {
            var result = await context.AuthenticateAsync(defaultAuthenticate.Name);
            if (result?.Principal != null)
            {
                context.User = result.Principal;
            }
        }

        await _next(context);
    }

    #endregion

    #region Properties

    /// 
    /// Scheme provider
    /// 
    public IAuthenticationSchemeProvider Schemes { get; set; }

    #endregion
}