User283571144 posted
Hi Beginner32,
According to your description,I suggest you could create a custom AuthorizeAttribute for your MVC controllers (or action methods) that checked the user’s .IsMemberOf method to see if the member belonged the sought after group (or groups).
Then if the user is not in the group couldn't access the controller or method.
Codes like below:
public class AuthorizeADAttribute : AuthorizeAttribute
{
private bool _authenticated;
private bool _authorized;
public string Groups { get; set; }
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
if (_authenticated && !_authorized)
{
filterContext.Result = new RedirectResult("/error/notauthorized");
}
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
_authenticated = base.AuthorizeCore(httpContext);
if (_authenticated)
{
if (string.IsNullOrEmpty(Groups))
{
_authorized = true;
return _authorized;
}
var groups = Groups.Split(',');
string username = httpContext.User.Identity.Name;
try
{
_authorized = LDAPHelper.UserIsMemberOfGroups(username, groups);
return _authorized;
}
catch (Exception ex)
{
this.Log().Error(() => "Error attempting to authorize user", ex);
_authorized = false;
return _authorized;
}
}
_authorized = false;
return _authorized;
}
}
Usage:
[AuthorizeAD(Groups="Some AD group name")]
public class HomeController : Controller
{
…
}
More details, you could refer to below article:'
http://www.benramey.com/2014/10/20/active-directory-authentication-in-asp-net-mvc-5-with-forms-authentication-and-group-based-authorization/
Best Regards,
Brando