Skip to content

Instantly share code, notes, and snippets.

@stefanolsen
Last active August 5, 2017 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanolsen/edf925b2a2daa77ccd38ca33e7d16093 to your computer and use it in GitHub Desktop.
Save stefanolsen/edf925b2a2daa77ccd38ca33e7d16093 to your computer and use it in GitHub Desktop.
UrlExtension for authorizing controllers and actions against a list of modules. Blog post at https://stefanolsen.com/posts/using-authorizeattribute-for-modules-in-a-multi-tenancy-web-platform/
public class AuthorizeModuleAttribute : AuthorizeAttribute
{
private readonly ModuleService _moduleService;
public string ModuleKey { get; set; }
public AuthorizeModuleAttribute()
{
_moduleService = new ModuleService();
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (string.IsNullOrWhiteSpace(ModuleKey))
{
return false;
}
string customerId = UserContext.CustomerId;
return customerId != null && _moduleService.HasEnabledModule(customerId, ModuleKey);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment