在C#中将SID转换为用户名

3
在.NET中,我可以使用域名和用户名创建一个NTAccount,并获取其SID。
但是,我无法使用翻译函数将SID转换回NTAccount
new SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString();

这个双向转换代码在域控制器上没有问题运行。

可能是一些配置出了问题?


可能是解决通过SID显示用户名的最佳方法?的重复问题。 - Edwin de Koning
2个回答

4

SecurityIdentifier.Translate()方法仅适用于域帐户,因此可能您的计算机未附加到域。要将本地SID解析为帐户名,您可以使用Win32 API函数LookupAccountSid(),例如查看这里


机器已使用域帐户登录到域中,该帐户在本地不存在。 - user970444
@user970444 嗯..那很奇怪,可能与访问域控制器有关的问题,无论如何,LookupAccountSid可以帮助您找到用户名,即使它不是本地帐户。 - Alexander

1

你可以使用.NET中的DirectoryServices,而不是使用SecurityIdentifier,这样会更容易且更通用。

在codeproject中,有一个很好的示例: http://www.codeproject.com/KB/cs/getusersid.aspx

代码如下:

private string GetSid(string strLogin)
{
    string str = "";
    // Parse the string to check if domain name is present.
    int idx = strLogin.IndexOf('\\');
    if (idx == -1)
    {
        idx = strLogin.IndexOf('@');
    }

    string strDomain;
    string strName;

    if (idx != -1)
    {
        strDomain = strLogin.Substring(0, idx);
        strName = strLogin.Substring(idx+1);
    }
    else
    {
        strDomain = Environment.MachineName;
        strName = strLogin;
    }


    DirectoryEntry obDirEntry = null;
    try
    {
        Int64 iBigVal = 5;
        Byte[] bigArr = BitConverter.GetBytes(iBigVal);
        obDirEntry = new DirectoryEntry("WinNT://" + 
                              strDomain + "/" + strName);
        System.DirectoryServices.PropertyCollection  
                           coll = obDirEntry.Properties;
        object obVal = coll["objectSid"].Value;
        if (null != obVal)
        {
            str = this.ConvertByteToStringSid((Byte[])obVal);
        }

    }
    catch (Exception ex)
    {
        str = "";
        Trace.Write(ex.Message);
    }
    return str;
}

private string ConvertByteToStringSid(Byte[] sidBytes)
{
    StringBuilder strSid = new StringBuilder();
    strSid.Append("S-");
    try
    {
        // Add SID revision.
        strSid.Append(sidBytes[0].ToString());
        // Next six bytes are SID authority value.
        if (sidBytes[6] != 0 || sidBytes[5] != 0)
        {
            string strAuth = String.Format
                ("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
                (Int16)sidBytes[1],
                (Int16)sidBytes[2],
                (Int16)sidBytes[3],
                (Int16)sidBytes[4],
                (Int16)sidBytes[5],
                (Int16)sidBytes[6]);
            strSid.Append("-");
            strSid.Append(strAuth);
        }
        else
        {
            Int64 iVal = (Int32)(sidBytes[1]) +
                (Int32)(sidBytes[2] << 8) +
                (Int32)(sidBytes[3] << 16) +
                (Int32)(sidBytes[4] << 24);
            strSid.Append("-");
            strSid.Append(iVal.ToString());
        }

        // Get sub authority count...
        int iSubCount = Convert.ToInt32(sidBytes[7]);
        int idxAuth = 0;
        for (int i = 0; i < iSubCount; i++)
        {
            idxAuth = 8 + i * 4;
            UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
            strSid.Append("-");
            strSid.Append(iSubAuth.ToString());
        }
    }
    catch (Exception ex)
    {
        Trace.Warn(ex.Message);
        return "";
    }
    return strSid.ToString();
}

文章中还介绍了从SID字节转换为字符串的方法。

1
问题是:SID -> 显示用户名 你的回答是:用户账户名 -> SID - Dennis C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接