2011-08-26 8 views
1

asp.net.In 프로젝트에서 하나의 웹 응용 프로그램을 만들었습니다. 인증은 database.But에서 사용자 이름과 암호를 일치 시켜서 수행되었습니다. 이제 클라이언트는 Active Directory의 도움으로 응용 프로그램에서 자동 로그인을 요청합니다. 입증. 고객이 인증을 위해 AD에서 사용자의 이메일 ID를 사용하도록 권유합니다. 더 나아가 IActive Directory 인증

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
    string[] a = Context.User.Identity.Name.Split('\\'); 

    System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]); 
    string Name = ADEntry.Properties["FullName"].Value.ToString(); 

:

가 나는 사용자의 전체 이름을 가져올 수 있지만, 나는 이메일 ID를 가져올 수 없습니다, 광고에 레코드를 가져 오기 위해 시도

나는 코드를 시도 DirectorySearcher를 사용하지만 Coulnot이 클라이언트 서버의 레코드를 검색하는 오류가 발생합니다 ..

답변

0

AD 데이터를 읽으려면이 클래스를 사용하십시오. 그것은 우리의 광고에 대한 설정이지만 기본적으로 찾을 수있는 모든 "필드"를 매개 변수로 전달할 수 있습니다. 그러나 이메일 주소를 보유하고있는 필드를 알아야합니다. Sysinternals는 AD를 탐색하고 ADExplorer라고하는 정보를 찾기 위해 꽤 좋은 도구를 만들었습니다.

하지만 왜 광고를보아야하는지 이해가 가지 않습니까? 그들이 네트워크에있는 경우 사용자가 이미 인증되었다고 가정하고 Windows ID에 의존 할 수 있습니까?

public static Hashtable GetAttributes(string initials, params string[] Attribute) 
{ 
    DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://ADNAME"); 
    DirectorySearcher ADSearcher = new DirectorySearcher(directoryEntry); 
    ADSearcher.Filter = "(sAMAccountName=" + initials + ")"; 
    foreach (string para in Attribute) 
    { 
     ADSearcher.PropertiesToLoad.Add(para); 
    } 
    SearchResult adSearchResult = ADSearcher.FindOne(); 

    Hashtable hshReturns = new Hashtable(); 
    foreach (string para in Attribute) 
    { 
     string strReturn = ""; 
     if (adSearchResult.Properties[para].Count == 0) 
      strReturn = ""; 
     else 
      strReturn = ((ResultPropertyValueCollection)adSearchResult.Properties[para])[0].ToString(); 
     hshReturns.Add(para, strReturn); 
    } 
    return hshReturns; 
} 
1

회사 포털을 만들면서 나는 똑같은 상황이었습니다. AD에 들어가기를 원하지 않는다면 포털에 액세스 할 수있는 사람들의 NT 로그인을 요청하는 것입니다. NTLogin을 가지고 있으며 포털에 액세스하고있는 시스템을 사용하여 간단하게 인증하는 간단한 테이블을 만드십시오. 내가 사용한 샘플 코드를 확인하십시오.

// Checking if the user opening this page is listed in the allowed user list against their NT login. 
     String sUser = Request.ServerVariables["LOGON_USER"].ToLower(); 
     sUser = sUser.Replace("wt\\", ""); 

     //Authentication using a custom auth method. 
     DatabaseOperations authenticateUser = new DatabaseOperations(); 
     if (!authenticateUser.authenticate(sUser)) 
     { 
      //unauthorized users will be redirected to access denied page. 
      Server.Transfer("AccessDenied.aspx", true); 
     } 

그리고 당신은 당신의 web.config 파일에서이 도움이

<authentication mode="Windows"></authentication> 

희망을 창에 인증 모드를 가지고 있는지 확인하고.