Skip to content

Instantly share code, notes, and snippets.

@avisser
Created April 22, 2016 14:17
Show Gist options
  • Save avisser/382e6d51450699fbb766e8a9b741e564 to your computer and use it in GitHub Desktop.
Save avisser/382e6d51450699fbb766e8a9b741e564 to your computer and use it in GitHub Desktop.
public class PutInGlobalAsaxCS {
protected void Application_PostAuthenticateRequest()
{
var user = HttpContext.Current.User as WindowsPrincipal;
if (user != null)
{
HttpContext.Current.User = new SpecPrincipal((WindowsIdentity) user.Identity);
}
}
}
public class SpecPrincipal : WindowsPrincipal
{
public SpecPrincipal(WindowsIdentity ntIdentity) : base(ntIdentity)
{
}
public override bool IsInRole(string role)
{
var roleMgr = DependencyResolver.Current.GetService<ApplicationRoleManager>();
var userMgr = DependencyResolver.Current.GetService<ApplicationUserManager>();
var user = userMgr.GetCurrentUser();
var rolesForUser = roleMgr.GetRolesForUser(user.Id);
return rolesForUser.Select(r => r.Name).Contains(role);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment