Webiant Logo Webiant Logo
  1. No results found.

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

WwwRequirementAttribute.cs

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Nop.Core;
using Nop.Core.Domain.Seo;
using Nop.Core.Http.Extensions;
using Nop.Data;

namespace Nop.Web.Framework.Mvc.Filters;

/// 
/// Represents a filter attribute that checks WWW at the beginning of the URL and properly redirect if necessary
/// 
public sealed class WwwRequirementAttribute : TypeFilterAttribute
{
    #region Ctor

    /// 
    /// Create instance of the filter attribute
    /// 
    public WwwRequirementAttribute() : base(typeof(WwwRequirementFilter))
    {
    }

    #endregion

    #region Nested filter

    /// 
    /// Represents a filter that checks WWW at the beginning of the URL and properly redirect if necessary
    /// 
    private class WwwRequirementFilter : IAsyncAuthorizationFilter
    {
        #region Fields

        protected readonly IWebHelper _webHelper;
        protected readonly SeoSettings _seoSettings;

        #endregion

        #region Ctor

        public WwwRequirementFilter(IWebHelper webHelper,
            SeoSettings seoSettings)
        {
            _webHelper = webHelper;
            _seoSettings = seoSettings;
        }

        #endregion

        #region Utilities

        /// 
        /// Check WWW prefix at the beginning of the URL and properly redirect if necessary
        /// 
        /// Authorization filter context
        /// Whether URL must start with WWW
        private void RedirectRequest(AuthorizationFilterContext context, bool withWww)
        {
            //get scheme depending on securing connection
            var urlScheme = $"{_webHelper.GetCurrentRequestProtocol()}{Uri.SchemeDelimiter}";

            //compose start of URL with WWW
            var urlWith3W = $"{urlScheme}www.";

            //get requested URL
            var currentUrl = _webHelper.GetThisPageUrl(true);

            //whether requested URL starts with WWW
            var urlStartsWith3W = currentUrl.StartsWith(urlWith3W, StringComparison.OrdinalIgnoreCase);

            //page should have WWW prefix, so set 301 (permanent) redirection to URL with WWW
            if (withWww && !urlStartsWith3W)
                context.Result = new RedirectResult(currentUrl.Replace(urlScheme, urlWith3W), true);

            //page shouldn't have WWW prefix, so set 301 (permanent) redirection to URL without WWW
            if (!withWww && urlStartsWith3W)
                context.Result = new RedirectResult(currentUrl.Replace(urlWith3W, urlScheme), true);
        }

        /// 
        /// Called early in the filter pipeline to confirm request is authorized
        /// 
        /// Authorization filter context
        private void CheckWwwRequirement(AuthorizationFilterContext context)
        {
            ArgumentNullException.ThrowIfNull(context);

            //only in GET requests, otherwise the browser might not propagate the verb and request body correctly.
            if (!context.HttpContext.Request.IsGetRequest())
                return;

            if (!DataSettingsManager.IsDatabaseInstalled())
                return;

            //ignore this rule for localhost
            if (_webHelper.IsLocalRequest(context.HttpContext.Request))
                return;

            switch (_seoSettings.WwwRequirement)
            {
                case WwwRequirement.WithWww:
                    //redirect to URL with starting WWW
                    RedirectRequest(context, true);
                    break;

                case WwwRequirement.WithoutWww:
                    //redirect to URL without starting WWW
                    RedirectRequest(context, false);
                    break;

                case WwwRequirement.NoMatter:
                    //do nothing
                    break;

                default:
                    throw new NopException("Not supported WwwRequirement parameter");
            }
        }

        #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 Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            CheckWwwRequirement(context);
            return Task.CompletedTask;
        }

        #endregion
    }

    #endregion
}