1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
string domainName = ConfigurationManager.AppSettings["DomainName"];
string groupsParam = ConfigurationManager.AppSettings["Groups"];
if ((domainName != null) && (groupsParam != null))
{
// Get the AD groups
var groups = groupsParam.Split(',').ToList<string>();
// Verify that the user is in the given AD group (if any)
var context = new PrincipalContext(ContextType.Domain, domainName);
var userPrincipal = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
httpContext.Request.LogonUserIdentity.Name);
foreach (var group in groups)
try
{
if (userPrincipal.IsMemberOf(context, IdentityType.Name, group))
return true;
}
catch (Exception)
{
continue;
}
}
return false;
} |
Partager