-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathRouteHelperExtensions.cs
38 lines (35 loc) · 1.39 KB
/
RouteHelperExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
namespace Fido2Demo
{
public static class RouteHelperExtensions
{
public static RewriteOptions AddRedirectToWWwIfPasswordlessDomain(this RewriteOptions options)
{
options.Add(new RedirectToWwwIfPasswordlessDomainRule());
return options;
}
public class RedirectToWwwIfPasswordlessDomainRule : IRule
{
public virtual void ApplyRule(RewriteContext context)
{
var req = context.HttpContext.Request;
if (req.Host.Host is "passwordless.dev" or "fido2.azurewebsites.net")
{
var wwwHost = new HostString("www.passwordless.dev");
var newUrl = UriHelper.BuildAbsolute("https", wwwHost, req.PathBase, req.Path, req.QueryString);
var response = context.HttpContext.Response;
response.StatusCode = 301;
response.Headers[HeaderNames.Location] = newUrl;
context.Result = RuleResult.EndResponse;
}
context.Result = RuleResult.ContinueRules;
return;
}
}
}
}