这必须从远程机器获得。下面的查询不是针对SID的,而是针对组名和帐户名的。
"SELECT GroupComponent FROM Win32_GroupUser WHERE PartComponent = \"Win32_UserAccount.Domain='" + accountDomain + "',Name='" + accountName + "'\""它返回的Win32_Group对象以字符串的形式出现,它们只有域名和名称(尽管Win32_Group具有SID属性)。
我有种下沉的感觉,我不得不:
发布于 2010-03-22 19:49:40
您能使用System.DirectoryServices.AccountManagement命名空间类吗?
using (var context = new PrincipalContext( ContextType.Domain ))
{
using (var user = UserPrincipal.FindByIdentity( context, accountName ))
{
var groups = user.GetAuthorizationGroups();
...iterate through groups and find SIDs for each one
}
}它应该与ContextType.Machine一起工作,尽管您需要指定机器名称并具有适当的特权。
using (var context = new PrincipalContext( ContextType.Machine,
"MyComputer",
userid,
password ))
{
...
}发布于 2010-03-22 19:56:50
是的,但是有些方法依赖于有一个域。
在ASP.NET应用程序中,可以使用这样的代码访问组信息,前提是用户通过Windows进行身份验证,而不是表单身份验证。在本例中,我留下了一个有趣的注释,介绍了在该环境中引发的异常,但它可能适用于其他用户:
public List<string> GetGroupsFromLogonUserIdentity()
{
List<string> groups = new List<string>();
HttpRequest request = HttpContext.Current.Request;
if (request.LogonUserIdentity.Groups != null)
{
foreach (IdentityReference group in request.LogonUserIdentity.Groups)
{
try
{
groups.Add(group.Translate(typeof(NTAccount)).ToString());
}
catch (IdentityNotMappedException)
{
// Swallow these exceptions without throwing an error. They are
// the result of dead objects in AD which are associated with
// user accounts. In this application users may have a group
// name associated with their AD profile which cannot be
// resolved in the Active Directory.
}
}
}
return groups;
}LogonUserIdentity基于WindowsIdentity类。您可以修改我的代码示例,以便在非Web应用程序中使用WindowsIdentity和函数。一旦您遍历了一个组,您就应该能够这样做来获得SecurityIdentifier:
SecurityIdentifier secid = group as SecurityIdentifier;https://stackoverflow.com/questions/2495185
复制相似问题