2011-09-29 7 views
3

.net에서 도메인과 사용자 이름을 사용하여 NTAccount을 만들고 SID를 얻을 수 있습니다.C에서 사용자 이름으로 SID를 변환하십시오 #

하지만 번역 기능을 사용하여 SID를 다시 NTAccount로 변환 할 수 없습니다.

new SecurityIdentifier(stringSid).Translate(typeof(NTAccount)).ToString(); 

그리고이 양방향 변환 코드는 도메인 컨트롤러에서 실행해도 문제가 없습니다.

일부 설정이 잘못 되었나요?

+0

가능한 복제본 [SID로 표시 사용자 이름을 확인하는 가장 좋은 방법은 무엇입니까?] (http://stackoverflow.com/questions/380031/the-best-way-to-resolve-display-username-by-sid) –

답변

4

SecurityIdentifier.Translate() 메서드는 도메인 계정에서만 작동하므로 컴퓨터가 도메인에 연결되어 있지 않을 수 있습니다. 로컬 SID를 계정 이름으로 확인하려면 Win32 API 함수 LookupAccountSid() look here을 사용할 수 있습니다.

+0

컴퓨터가 이미 도메인 계정을 사용하여 도메인에 로그인하면 해당 계정이 로컬에 존재하지 않습니다. – user970444

+0

@ user970444 음 ... 그 이상한 점은 도메인 컨트롤러에 대한 액세스 문제 일 수 있습니다. 어쨌든 LookupAccountSid는 로컬 계정이 아니더라도 사용자 이름을 찾는 데 도움이 될 수 있습니다. – Alexander

1

SecurityIdentifier를 사용하는 대신 .NET에서 DirectoryServices를보다 쉽고 일반적으로 사용할 수 있습니다. 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에서 변환이 문서에서 문자열로 바이트도있다 CodeProject의에서

이의 좋은 예제가있다.

+1

"easy"......;) – JoelFan

+0

질문 : SID -> 사용자 이름 표시 귀하가 대답했습니다 : 사용자 계정 이름 -> SID –

관련 문제