/// <summary> /// Determines whether the current user is in the specified group. /// </summary> /// <param name="userToken">The user token.</param> /// <param name="groupName">Name of the group.</param> /// <returns> /// <c>true</c> if the user blongs to the specified group; otherwise <c>false</c> /// </returns> public static bool IsUserInGroup(SafeTokenHandle userToken, string groupName) { var token = userToken.DangerousGetHandle(); if (token == IntPtr.Zero) { return(false); } using var id = new WindowsIdentity(token); var principal = new WindowsPrincipal(id); return(principal.IsInRole(groupName)); }
public static bool IsUserInGroup([NotNull] SafeTokenHandle userToken, [CanBeNull] string groupName) { Contract.Requires(userToken != null); var token = userToken.DangerousGetHandle(); if (token == IntPtr.Zero) { return(false); } using (var id = new WindowsIdentity(token)) { var principal = new WindowsPrincipal(id); return(principal.IsInRole(groupName)); } }