首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我有一个用户帐户的SID,我要它所属组的小岛屿发展中国家。

问我有一个用户帐户的SID,我要它所属组的小岛屿发展中国家。
EN

Stack Overflow用户
提问于 2010-03-22 19:44:45
回答 2查看 2K关注 0票数 7

这必须从远程机器获得。下面的查询不是针对SID的,而是针对组名和帐户名的。

代码语言:javascript
复制
"SELECT GroupComponent FROM Win32_GroupUser WHERE PartComponent = \"Win32_UserAccount.Domain='" + accountDomain + "',Name='" + accountName + "'\""

它返回的Win32_Group对象以字符串的形式出现,它们只有域名和名称(尽管Win32_Group具有SID属性)。

我有种下沉的感觉,我不得不:

  1. 通过查询Win32_SID将SID转换为帐户名称;
  2. 执行上述查询;
  3. 通过查询,将结果组名称的每个转换为SID。
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-03-22 19:49:40

您能使用System.DirectoryServices.AccountManagement命名空间类吗?

代码语言:javascript
复制
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一起工作,尽管您需要指定机器名称并具有适当的特权。

代码语言:javascript
复制
using (var context = new PrincipalContext( ContextType.Machine,
                                           "MyComputer",
                                           userid,
                                           password ))
{
   ...
}

但是,在使用新的文章 3.5帐户管理命名空间时,有一个不错的MSDN 文章(长)。

票数 3
EN

Stack Overflow用户

发布于 2010-03-22 19:56:50

是的,但是有些方法依赖于有一个域。

  1. 有关如何使用P/Invoke和Windows将SID转换为用户id,或者使用.NET 2.0+而不是P/Invoke,请参见此2.0+。 使用System.Security.Principal; //将用户sid转换为域\name字符串帐户=新SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString();
  2. 如果有AD和用户id,那么使用DirectorySearcher方法或Account管理API查找组。否则,使用这文章中概述的方法获取本地组。
  3. 现在,使用@tvanfosson建议的API来迭代组并获取SID。或者跟随下面的信息。

在ASP.NET应用程序中,可以使用这样的代码访问组信息,前提是用户通过Windows进行身份验证,而不是表单身份验证。在本例中,我留下了一个有趣的注释,介绍了在该环境中引发的异常,但它可能适用于其他用户:

代码语言:javascript
复制
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:

代码语言:javascript
复制
SecurityIdentifier secid = group as SecurityIdentifier;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2495185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档