I would like to selectively display a block of text on a page depending on which AD-Group a user is in. The page is a simple list of links ATM, no controller necessary. I have the code below which works perfectly when I am developing locally (I am logged onto the AD) - as soon I publish the application to an IIS server I get a 404 error - I have been able to locate the exact line that is causing the error -> in ActiveDirectory.IsInGroup () the line group.Translate is the culprit.
I have checked the Event Viewer on the IIS Server (and the log for IIS) but nothing is being logged at all?
This is the index.html:
@page
@using System.Security.Principal
@{
ViewData["Title"] = "Landing Page";
}
@{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool itUser = ActiveDirectory.IsInGroup(principal, "IT Department");
}
<h4>Header</h4>
@if (itUser || adminUser)
{
<div class="card mb-4 shadow-sm">
<div class="card-header">
<h4 class="my-0 font-weight-normal">IT</h4>
</div>
<div class="card-body">
<a target="_blank" href="http://www.test.com/Configuration/Index" class="btn btn-secondary my-2">Application Config</a><br />
</div>
</div>
}
here is the C# code:
public static class ActiveDirectory
public static bool IsInGroup(ClaimsPrincipal checkUser, string checkGroupName)
{
var identity = (WindowsIdentity)checkUser.Identity;
if (identity?.Groups == null) return false;
foreach (var group in identity.Groups)
{
var groupName = group.Translate(typeof(NTAccount)).ToString(); // this gives me a 404 error
if (groupName.ToLower().Contains(checkGroupName.ToLower())) return true;
}
return false;
}
}