Webiant Logo Webiant Logo
  1. No results found.

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

SignOutFromExternalAuthenticationAttribute.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Nop.Services.Authentication;

namespace Nop.Web.Framework.Mvc.Filters;

/// 
/// Represents filter attribute that sign out from the external authentication scheme
/// 
public sealed class SignOutFromExternalAuthenticationAttribute : TypeFilterAttribute
{
    #region Ctor

    /// 
    /// Create instance of the filter attribute
    /// 
    public SignOutFromExternalAuthenticationAttribute() : base(typeof(SignOutFromExternalAuthenticationFilter))
    {
    }

    #endregion

    #region Nested filter

    /// 
    /// Represents a filter that sign out from the external authentication scheme
    /// 
    private class SignOutFromExternalAuthenticationFilter : IAsyncAuthorizationFilter
    {
        #region Utilities

        /// 
        /// Called early in the filter pipeline to confirm request is authorized
        /// 
        /// Authorization filter context
        /// A task that represents the asynchronous operation
        private async Task SignOutFromExternalAuthenticationAsync(AuthorizationFilterContext context)
        {
            ArgumentNullException.ThrowIfNull(context);

            //sign out from the external authentication scheme
            var authenticateResult = await context.HttpContext.AuthenticateAsync(NopAuthenticationDefaults.ExternalAuthenticationScheme);
            if (authenticateResult.Succeeded)
                await context.HttpContext.SignOutAsync(NopAuthenticationDefaults.ExternalAuthenticationScheme);
        }

        #endregion

        #region Methods

        /// 
        /// Called early in the filter pipeline to confirm request is authorized
        /// 
        /// Authorization filter context
        /// A task that represents the asynchronous operation
        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            await SignOutFromExternalAuthenticationAsync(context);
        }

        #endregion
    }

    #endregion
}