2008-11-24 9 views
6

ASP.NET 응용 프로그램 내에서 Windows 인증을 사용하고 있습니다. 나는 현재 로그인 한 사용자로부터 objectGuid를 가장 잘 얻는 방법을 알고 싶습니다.AD 사용자 인증 asp.net의 objectGuid

감사합니다, Egil.

답변

4

System.DirectoryServices 네임 스페이스로이 작업을 수행 할 수 있습니다.

Dim entry As DirectoryServices.DirectoryEntry 
Dim mySearcher As System.DirectoryServices.DirectorySearcher 
Dim result As System.DirectoryServices.SearchResult 
Dim myEntry As DirectoryEntry 
Dim domainName As String 
Dim userId As String 
Dim objectGuid As Guid 

'Split the username into domain and userid parts 
domainName = Page.User.Identity.Name.Substring(0, Page.User.Identity.Name.IndexOf("\")) 
userId = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1) 

'Start at the top level domain 
entry = New DirectoryEntry(domainName) 

mySearcher = New DirectorySearcher(entry) 

'Build a filter for just the user 
mySearcher.Filter = ("(&(anr=" & userId & ")(objectClass=user))") 

'Get the search result ... 
result = mySearcher.FindOne 

'... and then get the AD entry that goes with it 
myEntry = result.GetDirectoryEntry 

'The Guid property is the objectGuid 
objectGuid = myEntry.Guid 

이렇게하는 것이 더 좋은 방법 일 수 있습니다.

+0

내일 볼게요. 감사. –

+0

고맙습니다. 올바른 objectGuid를 얻으려면 다음 코드를 대신 사용하십시오. objectGuid = System.Guid.Parse (myEntry.NativeGuid) – geekinit

2

NativeGuid 속성을 사용해야합니다. C# 코드 :

string login = HttpContext.Current.User.Identity.Name; 
string domain = login.Substring(0, login.IndexOf('\\')); 
string userName = login.Substring(login.IndexOf('\\') + 1); 
DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + domain); 
DirectorySearcher searcher = new DirectorySearcher(domainEntry); 
searcher.Filter = string.Format(
    "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))", 
    userName); 
SearchResult searchResult = searcher.FindOne(); 
DirectoryEntry entry = searchResult.GetDirectoryEntry(); 
Guid objectGuid = new Guid(entry.NativeGuid); 
11

제안 솔루션은 다소 비쌉니다. 도메인 및 사용자 이름별로 검색하는 대신 SID를 사용하여 계정을 조회하는 것이 더 좋습니다.

// using System.Security.Principal; 
IPrincipal userPrincipal = HttpContext.Current.User; 
WindowsIdentity windowsId = userPrincipal.Identity as WindowsIdentity; 
if (windowsId != null) 
{ 
    SecurityIdentifier sid = windowsId.User; 

    using(DirectoryEntry userDe = new DirectoryEntry("LDAP://<SID=" + sid.Value + ">")) 
    { 
     Guid objectGuid = new Guid(userDe.NativeGuid); 
    } 
} 
+0

windowsId가 null 인 경우 어떻게해야합니까? – dpp

+0

그런 다음 익명 사용자 또는 Kerberos 인증을 허용하도록 웹 사이트를 신뢰하지 않는 사용자를 상대하고있는 것 같습니다. 회사의 관리자와 함께 그룹 정책을 추진하여 * 내부 * 사이트를 신뢰할 수 있습니다. 또는 https 로그인 양식이나 기타 인증 방법을 제공 할 수도 있습니다. – Felan

관련 문제