MVC Authorize Attribute Work without Loging off

Grizzly

I have a controller class that only a specific Active Directory Group should be able to access.

[Authorize(Roles = @"Domain\GroupName")]
public class AdminToolsController : Controller
{
    ...
}

Now as I am testing.. I am currently out of the group.. but if I add myself.. and I try to access anything in this controller I still get asked to login and my credentials do not work. However.. if I add myself.. then logoff.. then log back on.. then try to access anything in this controller it recognizes me and allows me access.

Is there anyway to do this instantaneously? Meaning, can I add myself to the group and successfully access any of the methods inside the controller without having to logoff and log back on?

UPDATE

I have edited Camilo Terevinto answer below. Their answer for some reason.. whenever I added or removed my self from the specific group.. that group would not be a part of the variable groups.

Here is my update:

public class AuthorizeByActiveDirectoryGroups : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var roles = Roles.Split(',');
        using (var domainContext = new PrincipalContext(ContextType.Domain, "domainname"))
        {
            var user = httpContext.User.Identity.Name;
            using (var domainUser = UserPrincipal.FindByIdentity(domainContext, httpContext.User.Identity.Name))
            {
                var adgroup = GroupPrincipal.FindByIdentity(domainContext, "Domain\\GroupName");
                bool member = domainUser.IsMemberOf(adgroup);
                var groups = domainUser.GetAuthorizationGroups();
                return member;
            }
        }
    }
}
Camilo Terevinto

IIRC, the roles are only obtained once when you log in with the default Windows authentication, so in order to always get the latest you could use a custom attribute.
Since this would always check the AD values, you could use some caching and only refresh the values when required, but that depends on your specific case.

Note: I don't have VS right now so there might be some spelling issue

public class AuthorizeByActiveDirectoryGroupsAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var roles = Roles.Split(",");
        using (var domainContext = new PrincipalContext(ContextType.Domain))
        {
            using (var domainUser = UserPrincipal.FindByIdentity(domainContext, httpContext.User.Identity.Name))
            {
                var groups = domainUser.GetAuthorizationGroups();
                return groups
                    .Select(x => x.Name) // the group name
                    .Any(x => roles.Contains(x)); // any group is one of the specified in the Roles property of the attribute
            }
        }
    }
}

So you would then use it like:

[AuthorizeByActiveDirectoryGroups(Roles = "Group1,Group2")]
public ActionResult Index()
{
    return View();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Authorize Attribute with Multiple Roles

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

Authorize attribute not working MVC 5

How to create a custom attribute that will redirect to Login if it returns false, similar to the Authorize attribute - ASP.NET MVC

Redirect loop with .Net MVC Authorize attribute with ADFS Claims

PrincipalPermission vs Authorize Attribute?

SignalR Authorize attribute does not work with cookie authentication

Use [Authorize] Attribute Without Identity?

Authorize attribute on entire project in .net mvc for security

Extending the Authorize attribute

HandleAuthenticateAsync called even without [Authorize] attribute

loging response from server does not work

Capture exceptions from Authorize attribute in MVC4

Check authorize in SignalR attribute

MVC5 Authentication: Authorize attribute on every controller or base controller

Authorize attribute and changed database

MVC5 [Authorize] redirecting to default route instead of attribute route

Roles in Authorize Attribute does not work as expected in MVC 4

Custom Authorize Attribute on asp.net mvc

Regarding Authorize attribute usage in ASP.Net MVC 4

Authorize attribute of custom Role provider not working in MVC 5

Is there a way to edit bashrc without loging into a user account

Mvc Authorize attribute is not working

Authorize if not in specific role attribute MVC 5

Asp.net Core MVC Authorize Attribute not blocking

MVC Authorize Attribute One Time In Controller

How can I turn off automatic checking loging state in blazor?

How to loging to django with login form? With or without Ajax

Custom Authorize Attribute show denied message without routing to controller