Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
using System;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Hexasoft
{
public class BasicAuthentication : IHttpModule
{
static Regex requirePathRegex;

public void Init(HttpApplication context)
{
context.BeginRequest += ContextBeginRequest;

var regexRaw = ConfigurationManager.AppSettings["BasicAuthentication.RequiredOnPathRegex"];
var ignoreCaseRaw = ConfigurationManager.AppSettings["BasicAuthentication.RequiredOnPathRegex.IgnoreCase"];

if (!string.IsNullOrEmpty(regexRaw))
{
var options = RegexOptions.None;

if (string.Equals(ignoreCaseRaw, "true", StringComparison.InvariantCultureIgnoreCase) || ignoreCaseRaw == "1")
{
options |= RegexOptions.IgnoreCase;
}

requirePathRegex = new Regex(regexRaw, options);
}
}

private void ContextBeginRequest(object sender, EventArgs e)
Expand Down Expand Up @@ -38,6 +56,10 @@ private bool Required()
requiredSetting = requiredSetting.Trim().ToLower();
required = requiredSetting == "1" || requiredSetting == "true";
}
else if (requirePathRegex != null)
{
required = requirePathRegex.IsMatch(HttpContext.Current.Request.Url.AbsolutePath);
}

return required;
}
Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ After installing the package you will see 3 new settings in the `appSettings` se

Use the `BasicAuthentication.Required` to quickly turn the authentication on or off while the username/password settings are self explanatory. Username is case-insensitive, password is case-sensitive.

As an alternative to securing the entire site, you can specify a regular expression to match against the URL Path by replacing the `"BasicAuthentication.Required"` appSetting with something like the following:

<add key="BasicAuthentication.RequiredOnPathRegex" value="^\/my-protected-path" />
<add key="BasicAuthentication.RequiredOnPathRegex.IgnoreCase" value="true" />

As this module was meant to secure WebApi's behind an Azure API Management, it only supports one username/password combination. No support for multiple users,


Expand Down