Last post Aug 25, 2015 02:34 PM by jkjhse
Aug 10, 2015 05:03 AMemail@example.com|LINK
i want to get group names from Active Directory based on User ID, means i want to know UserA associates with which groups?
Aug 10, 2015 05:15 AM|PatriceSc|LINK
It's a bit unclear if this is to support your app or if you create rather a user account administration app (ie UserID is the connected user or it could be some other user?).
If using Windows authentication,
https://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal.isinrole(v=vs.110).aspx should work.
For more Advanced needs see the https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement(v=vs.110).aspx namespace
for specialized AD classes.
Aug 10, 2015 04:48 PM|jkjhse|LINK
group names from Active Directory based on User ID
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); //Getting all groups but only for logged in Sub Domain in the forest
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, r.UserName);
if (user != null)
// find the roles....
var roles = user.GetAuthorizationGroups();
var Email = user.EmailAddress;
var UserName = user.SamAccountName;
var LastLog = (DateTime)user.LastLogon;
var Designation = user.Description;
var Name = user.DisplayName;
var LastBadPass = (DateTime)user.LastBadPasswordAttempt;
var PassChange = (DateTime)user.LastPasswordSet;
// enumerate over the roles
foreach (Principal p in roles)
<span> @p.ToString(); </span>
<p> EMail: @Email</p>
<p> UserName: @UserName</p>
<p>Password Changed: @PassChange.ToLocalTime().ToString("dd/MM/yyyy HH:mm") </p>
<p>Last Login : @LastLog.ToLocalTime().ToString("dd/MM/yyyy HH:mm")</p>
<p> Designation : @Designation </p>
<p>Name : @Name </p>
<p> Last Login Failure : @LastBadPass.ToLocalTime().ToString("dd/MM/yyyy HH:mm") </p>
Hope thius helps.. Adjust the Reference calls
Aug 12, 2015 04:43 PMfirstname.lastname@example.org|LINK
i tried your code but exception at this place "
user.GetAuthorizationGroups()error says that there is no authorized with this user but, it has many groups associated with the user
Aug 13, 2015 05:38 AMemail@example.com|LINK
Aug 18, 2015 05:30 AM|Lorris|LINK
From my point of view, I suggest you could add a property (GroupName) for the User object. When you add a user into a group, you could set the GroupName property. Then, you could get user's group name by the GroupName property.
Aug 25, 2015 02:34 PM|jkjhse|LINK