NikasBelogolov

C# WWW Middleware

Dec 24th, 2020 (edited)
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Text.RegularExpressions;
  4.  
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Http.Extensions;
  7. using Microsoft.AspNetCore.Builder;
  8.  
  9. using Microsoft.Extensions.Logging;
  10.  
  11.  
  12. namespace Website.Middlewares
  13. {
  14.  
  15.     public class WWWRedirectionMiddleware
  16.     {
  17.         private readonly RequestDelegate _next;
  18.         private readonly ILogger _logger;
  19.  
  20.         private string _domain;
  21.         private int _https_port;
  22.  
  23.         public WWWRedirectionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
  24.         {
  25.             _next = next ?? throw new ArgumentNullException(nameof(next));
  26.             _logger = loggerFactory.CreateLogger<WWWRedirectionMiddleware>();
  27.         }
  28.  
  29.         public async Task InvokeAsync(HttpContext context)
  30.         {
  31.  
  32.             var request = context.Request;
  33.             var response = context.Response;
  34.  
  35.             var host = request.Host;
  36.  
  37.             if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
  38.             {
  39.                 _domain = "localhost";
  40.                 _https_port = 5001;
  41.             }
  42.             else
  43.             {
  44.                 _domain = "seaoftools.com";
  45.                 _https_port = 80;
  46.             }
  47.  
  48.             // Redirect to www domain if url is home page
  49.             if (host.Host.Equals(_domain, StringComparison.OrdinalIgnoreCase))
  50.             {
  51.                 host = new HostString("www." + host.Host, _https_port);
  52.  
  53.                 var redirectUrl = UriHelper.BuildAbsolute(
  54.                     "https",
  55.                     host,
  56.                     request.PathBase,
  57.                     request.Path,
  58.                     request.QueryString);
  59.  
  60.                 response.Redirect(redirectUrl, true);
  61.                 return;
  62.             } // Redirect to non-www domain if url is with a subdomain
  63.             else if (host.Value.StartsWith("www.", StringComparison.OrdinalIgnoreCase)
  64.                      && Regex.Replace(host.Host, @"www.|" + _domain, String.Empty) != String.Empty)
  65.             {
  66.                 host = new HostString(host.Host.Replace("www.", String.Empty), _https_port);
  67.  
  68.                 var redirectUrl = UriHelper.BuildAbsolute(
  69.                     "https",
  70.                     host,
  71.                     request.PathBase,
  72.                     request.Path,
  73.                     request.QueryString);
  74.  
  75.                 response.Redirect(redirectUrl, true);
  76.                 return;
  77.             }
  78.  
  79.             await _next.Invoke(context);
  80.         }
  81.     }
  82.  
  83. }
Add Comment
Please, Sign In to add comment