2016-12-08 4 views
0

아래의 코드를 사용하여 로그인 한 사용자에 대한 다양한 정보를 얻고 작동하지만 원하는 속성을 전달할 수있는 방법으로 바꾸고 싶습니다. DisplayName, EmailAddress 등) 그리고 누구든지 로그인 한 상태에서이 값을 반환합니다. 어떻게 작동 시킬지 알 수 없었습니다.LDAP/AD에서 사용자 세부 정보를 얻는 방법

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context, User.Identity.Name)) 
    { 
     if (user != null) 
     { 
      loggedInUserfullName = user.DisplayName; 
      loggedInUserEmail = user.EmailAddress; 
     } 
    } 
} 

위의 코드는 작동하지만 메소드의 범위를 벗어나는 변수는 사용할 수 없으므로 유용하지 않습니다. 난 그냥 DisplayName를 얻기 위해 아래의 방법을 시도했지만 FindByIdentity 문자열 기대로 인해 오류가있어

(I 사용자의 이름을 가진 문자열이없는은 - 그게 내가 찾아 노력하고있어입니다!)

public string getUserFullName(object user) 
{ 
    user = UserPrincipal.FindByIdentity(ContextType.Domain, user); 
    string loggedInUserDisplayName = user.DisplayName; 
    return loggedInUserDisplayName ; 
} 

UserPrincipal loggedInUser = new UserPrincipal(ContextType.Domain); 
// line above throws cannot convert from 'System.DirectoryServices.AccountManagement.ContextType' to 'System.DirectoryServices.AccountManagement.PrincipalContext' 
getUserDetails(loggedInUser); 

이 방법이 있습니까? 내가 원하는 것을 얻기 위해 잘못된 라이브러리를 사용하고 있습니까?

Windows 인증을 사용하는 웹 응용 프로그램입니다.

감사

+0

System.DirectoryServices.AccountManagement를 사용하여 :

여기
public static UserInfo GetDisplayName(string name) { using (UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), name)) { if (user != null) { return new UserInfo { FullName = user.DisplayName, Email = user.EmailAddress, GivenName = user.GivenName, SamAccountName = user.SamAccountName, Surname = user.Surname //any other things you may need somewhere else }; } throw new Exception("error"); } } 

는 사용자 정보 클래스 :

편집

여러 필드를 포함하는 객체를 반환하려면, 당신은 같은 뭔가를 할 수 ; 당신은 이것을 가지고 있습니까 – MethodMan

+0

예, 그것이 프로젝트에 포함되어 있습니다. –

답변

2

나는 오랜 시간에 DirectoryServices.AccountManagement 물건을 사용하지 않은,하지만 난이 탄 줄 것이다.

UserPrincipal loggedInUser = new UserPrincipal(ContextType.Domain); 

예외가 UserPrincipal의 생성자가 System.DirectoryServices.AccountManagement.PrincipalContext 기대하고 있음을 말하고있다,하지만 당신은 그것을 시스템을 제공하고 있습니다 :

코드에서이 줄

는 예외를 던지고있다. DirectoryServices.AccountManagement.ContextType. 당신의 작업 코드에서이 선은 올바른 :

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context, User.Identity.Name)) 
    { 

나는 완전히 당신의 의도가 무엇인지 이해 모르겠지만, 당신은 같은 사용자 로그인 시도 뭔가에 대한 정보를 얻을 수있는 재사용 가능한 방법을 찾고 있다면 이 :

public static class UserManager 
{ 
    public static string GetDisplayName(string name) 
    { 
     using (UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), name)) 
     { 
      if (user != null) 
      { 
       return user.DisplayName; 
      } 

      throw new Exception("error"); 
     } 
    } 
} 

이 작업을 수행하여 호출 할 수 있습니다 :

var dn = UserManager.GetDisplayName(User.Identity.Name); 

물론, 당신은 더 나은 오류를 처리 할 수 ​​있습니다. 내가 뭔가를 놓친다면 알려주고 대답을 업데이트하려고 노력할 것입니다.

희망이 도움이됩니다.

public class UserInfo 
{ 
    public string FullName { get; set; } 
    public string Email { get; set; } 

    public string Surname { get; set; } 

    public string GivenName { get; set; } 

    public string SamAccountName { get; set; } 
} 
+0

감사합니다. UserManager.getattribute (attribute, name) 또는 이와 비슷한 메소드를 호출하는 것처럼 좋겠지 만 ID로 보입니다. 전자 메일, 표시 이름 또는 필요한 Active Directory 속성 중 하나 인 특성 이것이 가능한가? –

+0

디버거를 사용하면 SamAccount 이름 등을 얻을 수 있어야합니다. 적어도 @ JayF1을 시도해 보셨습니까? – MethodMan

+0

@ JayF1 특정 필드를 동적으로 반환하는 방법을 원한다면 할 수 있다는 것을 모릅니다. 내가 제안 할 수있는 것은 당신이 필요로하는 필드를 가지고 있고 콜에서 그 객체를 반환하는 클래스를 만드는 것입니다. 그런 다음 호출하는 메소드에서 필요한 필드를 얻을 수 있습니다. –

관련 문제