문제

나는 C#응용 프로그램을 검사하는 디렉토리와 모 몇 가지 정보를 제공합니다.고 싶을 표시하는 계정의 이름을 각자 파일입니다.내가 이것을 할 수 있는 로컬 시스템에서 얻어 SID FileInfo 및 하:

string GetNameFromSID( SecurityIdentifier sid )
{
    NTAccount ntAccount = (NTAccount)sid.Translate( typeof( NTAccount ) );
    return ntAccount.ToString();
}

그러나,이것은 작동하지 않는 파일에 대한 네트워크에서,아마도 때문에 번역()함수와 함께 작동 지역의 사용자 계정입니다.내 생각에는 어쩌면 내가 할 수 있는 LDAP 조회에서 시드,그래서 내가려고 다음과 같다:

string GetNameFromSID( SecurityIdentifier sid )
{
    string str = "LDAP://<SID=" + sid.Value + ">";
    DirectoryEntry dirEntry = new DirectoryEntry( str );
    return dirEntry.Name;
}

이처럼 보인 그것은 작품에 대한 액세스"dirEntry.이름이"중단,몇 초 동안 같은 경우가 및 쿼리 네트워크,하지만 그것은 던졌 시스템입니다.런타임입니다.InteropServices.발

누군가는 방법을 알고 나를 얻을 수 있는 계정의 이름을 임의 파일 또는 SID?나는에 대해 많이 알지 못한 네트워킹 또는 LDAP 또는 아무것도.이라는 클래스때는 어쩌면 하는데 사용할 수 있지만,그것은 원하는 도메인 이름을 내가 알지 못하는 방법 중 하나는 모든이 있는 디렉토리의 경로 나 검사입니다.

미리 감사드립니다.

도움이 되었습니까?

해결책

SecurityReference Object의 Translate Method는 비 로컬 SIDS에서만 작동하지만 도메인 계정에서만 작동합니다. 다른 기계에 로컬 또는 비 도메인 설정으로 계정의 경우, COLUPACCOUNTOUTSID를 수행 해야하는 특정 기계 이름을 지정하는 기능을 수행해야합니다.

다른 팁

좋은 답변은 여기를 참조하십시오.

SID의 디스플레이 사용자 이름을 해결하는 가장 좋은 방법은?

그것의 요점은이 비트입니다.

string sid="S-1-5-21-789336058-507921405-854245398-9938";
string account = new System.Security.Principal.SecurityIdentifier(sid).Translate(typeof(System.Security.Principal.NTAccount)).ToString();

이 접근법은 로컬이 아닌 SID가 Active Directory를 통해 저에게 효과적입니다.

System.DirectoryServices.AccountManagement.UserPrincipal 수업 (MSDN 링크)는 정적 기능이 있습니다 FindByIdentity SID를 사용자 객체로 변환합니다. 로컬 컴퓨터 또는 LDAP/Active Directory 서버에 대해 모두 작동 할 수 있어야합니다. Active Directory에 대해서만 사용했습니다.

다음은 IIS에서 사용한 예입니다.

// Set the search context to a specific domain in active directory
var searchContext = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");
// get the currently logged in user from IIS
MembershipUser aspUser = Membership.GetUser();
// get the SID of the user (stored in the SecurityIdentifier class)
var sid = aspUser.ProviderUserKey as System.Security.Principal.SecurityIdentifier;
// get the ActiveDirectory user object using the SID (sid.Value returns the SID in string form)
var adUser = UserPrincipal.FindByIdentity(searchContext, IdentityType.Sid, sid.Value);
// do stuff to user, look up group membership, etc.

C#에서 사용자 SID를 가져 와서 다음을 통해 문자열 변수에 할당하십시오.

string strUser = System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();

사용자 이름이 string을 지원하는 기능이 있으므로 String을 사용해야합니다. 즉, var varuser를 사용하면 네임 스페이스 오류가 발생합니다.

string strUserName = new System.Security.Principal.SecurityIdentifier(strUser).Translate(typeof(System.Security.Principal.NTAccount)).ToString();

OOH, 그러면 LDAP 호출이 작동하지 않을 수 있습니다. 이 경우 각 기계는 자체 ID 스토어를 담당합니다. 또한 코드를 실행하는 시스템은 원격 시스템에서만 의미가있는 SID를 해결하는 방법을 모르기 때문에 첫 번째 코드 샘플이 네트워크를 통해 작동하지 않습니다.

기계가 액티브 디렉토리의 일부인지 실제로 확인해야합니다. 로그온 과정에서 이것을 알게 될 것입니다. 또는 "내 컴퓨터"를 마우스 오른쪽 버튼으로 클릭하고 "컴퓨터 이름"탭 인 "Properties"를 선택한 다음 컴퓨터가 도메인의 일부인지 확인할 수 있습니다.

엄청난. 나는 여기에서 약간의 kookupaccountsid () 코드를 crib습니다.

http://www.pinvoke.net/default.aspx/advapi32.lookupaccountsid

그리고 나는 호스트 이름을 직접 제공해야했지만 효과가있었습니다. UNC 경로의 경우 첫 번째 구성 요소를 취할 수 있습니다. 매핑 된 드라이브 인 경우이 코드를 사용하여 경로를 UNC로 변환합니다.

http://www.wiredprairie.us/blog/index.php/archives/22

UNC 경로의 첫 번째 구성 요소가 호스트 이름이 아닌 상황을 제시하지 않는 한 누군가가 효과가있는 것 같습니다.

도와 주셔서 감사합니다.

또한 사용자의 언어 설정에 관계없이 작동하는 코드와 같은 "Everybody"와 같은 특수 계정의 계정 이름을 얻을 수 있습니다.

   SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
   string everyone = everyoneSid.Translate(typeof(System.Security.Principal.NTAccount)).ToString();

이것은 스무퍼입니다. 당신은 적극적인 디렉토리 환경에 있습니까? 그냥 확인 :)

어쨌든 sid.value와의 바인딩 대신

string str = "LDAP://<SID=" + sid.Value + ">";

SID의 바이트 배열을 문어 문자열 대신 그것과 묶습니다.

달콤한 예가 있습니다 여기 78 페이지에. 이것은 당신을 더 가까워 질 것입니다. 솔직히 말해서, 나는 전에 SID와의 바인딩을 시도하지 않았습니다. 그러나 나는 사용자의지도와 성공을 거두었습니다. :)

행운을 빕니다. 어떻게 진행되는지 알려주세요.

현재 도메인:

System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();

디렉토리 항목에서 ldap 및 도메인 이름:

DirectoryEntry de = new DirectoryEntry(string.Format("LDAP://{0}", domain));

을 얻을 sid 에서 ActiveDirectoryMembershipProvider ActiveDirectoryMembershipUser:

ActiveDirectoryMembershipUser user = (ActiveDirectoryMembershipUser)Membership.GetUser();
var sid = (SecurityIdentifier)user.ProviderUserKey;

을 얻을에서 사용자 이름 SecurityIdentifier:

(NTAccount)sid.Translate(typeof(NTAccount));

을 얻을 디렉토리 검색에 수행 active directory 도메인과 디렉토리 항목과 사용자 이름:

DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = string.Format("(SAMAccountName={0})", username);
        search.PropertiesToLoad.Add("Name");
        search.PropertiesToLoad.Add("displayName");
        search.PropertiesToLoad.Add("company");
        search.PropertiesToLoad.Add("homePhone");
        search.PropertiesToLoad.Add("mail");
        search.PropertiesToLoad.Add("givenName");
        search.PropertiesToLoad.Add("lastLogon");
        search.PropertiesToLoad.Add("userPrincipalName");
        search.PropertiesToLoad.Add("st");
        search.PropertiesToLoad.Add("sn");
        search.PropertiesToLoad.Add("telephoneNumber");
        search.PropertiesToLoad.Add("postalCode");
        SearchResult result = search.FindOne();
        if (result != null)
        {
            foreach (string key in result.Properties.PropertyNames)
            {
                // Each property contains a collection of its own
                // that may contain multiple values
                foreach (Object propValue in result.Properties[key])
                {
                    outputString += key + " = " + propValue + ".<br/>";
                }
            }
        }

에 따라에서 데이터의 활성 디렉토리로,당신은 당신을 얻을 것이 다양한 응답에서 출력됩니다.

여기에는 사이트에 있는 모든 사용자 등록 정보가 필요하다:

여기에서 허용 된 답변을 사용할 수있을 것입니다. C#을 사용하여 LocalSystem 계정 이름을 결정하십시오.

기본적으로 SecurityIndifier 클래스의 인스턴스를 NTACCount를 입력하여 사용자 이름을 얻을 수 있습니다. 코드 :

using System.Security.Principal;

SecurityIdentifier sid = new SecurityIdentifier("S-1-5-18");
NTAccount acct = (NTAccount)sid.Translate(typeof(NTAccount));
Console.WriteLine(acct.Value);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top