2013-06-06 1 views
2

ActiveDirectory 사용자 개체 터미널 서비스 속성을 읽고 쓸 필요가 있습니다. 나는이 시도 :msTSProfilePath, msTSHomeDirectory 등을 사용하여 C# DirectoryServices UserContext를 확장하는 방법

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA"); 

     using (context) 
     { 
      UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic"); 
      if (user != null) 
      { 
       DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject(); 
       entry.Properties["msTSProfilePath"].Value = ""; 
       entry.Properties["msTSHomeDirectory"].Value = ""; 
       entry.Properties["msTSHomeDrive"].Value = ""; 
       entry.CommitChanges(); 
      } 

     } 

을 그리고 나는이 시도 :

PrincipalContext context = new PrincipalContext(ContextType.Domain, "CA"); 

     using (context) 
     { 
      UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "CA\\vlekovic"); 
      if (user != null) 
      { 
       DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject(); 
       entry.InvokeSet("msTSProfilePath", ""); 
       entry.InvokeSet("msTSHomeDirectory", ""); 
       entry.InvokeSet("msTSHomeDrive", ""); 
       entry.CommitChanges(); 
      } 

     } 

을하지만 아무것도 작동하지 않습니다.

나는 속성 이름을 다음과 같이 또한 시도 :

  • TerminalServicesProfilePath
  • TerminalServicesHomeDirectory
  • TerminalServicesHomeDrive

그러나 행운. 어떤 도움을 주시면 감사하겠습니다!

감사합니다, 보진

+0

당신은 위의 코드는하지 않았다 말할 때 일하는거야, 너는 정교 할 수 있니? 예를 들어 예외가 발생 했습니까? – Nate

+0

예, 다음 오류가 발생합니다. DirectoryServiceCOMExtension이 처리되지 않았습니다. 디렉터리 서비스에 지정된 특성 구문이 유효하지 않습니다. – Vojin

답변

0

당신이 .NET 3.5 위로와 System.DirectoryServices.AccountManagement (S.DS.AM) 네임 스페이스를 사용중인 경우 쉽게 고급 속성을 얻기 위해 기존 UserPrincipal 클래스를 확장 할 수 있습니다, 같은 Manager

는 여기에 대해 모든 읽기 :

기본적으로, 당신은 단지 UserPrincipal를 기반으로 파생 된 클래스를 정의하고 당신이 원하는 당신의 추가 속성을 정의 :

[DirectoryRdnPrefix("CN")] 
[DirectoryObjectClass("Person")] 
public class UserPrincipalEx : UserPrincipal 
{ 
    // Implement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context) 
    { } 

    // Implement the constructor with initialization parameters.  
    public UserPrincipalEx(PrincipalContext context, 
         string samAccountName, 
         string password, 
         bool enabled) : base(context, samAccountName, password, enabled) 
    {} 

    // Create the "TermSrvProfilePath" property.  
    [DirectoryProperty("msTSProfilePath")] 
    public string TermSrvProfilePath 
    { 
     get 
     { 
      if (ExtensionGet("msTSProfilePath").Length != 1) 
       return string.Empty; 

      return (string)ExtensionGet("msTSProfilePath")[0]; 
     } 
     set { ExtensionSet("msTSProfilePath", value); } 
    } 
} 

지금, 당신은에 UserPrincipalEx의 "확장"버전을 사용할 수 있습니다 코드 :

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser"); 

    // you can easily access the TermSrvProfilePath now 
    string path = inetPerson.TermSrvProfilePath; 
}   

같은 식으로, 당신은 당신이 찾고있는 다른 속성을 추가 할 수 있습니다.

+0

오류 : PrincipalOperationException이 발견되었습니다. - 디렉토리 서비스에 지정된 속성 구문이 유효하지 않습니다. TerminalServicesProfilePath 속성 이름을 변경하면 다음 오류가 발생합니다. PrincipalOperationException 처리되지 않았습니다 : 지정한 디렉터리 서비스 특성 또는 값이 존재하지 않습니다. – Vojin

0

나는 당신이 우리에게 말한 것과 똑같은 방식으로했습니다.

나는 항상 반환 "빈"

[DirectoryProperty("wWWHomePage")] 
 
    public string wWWHomePage 
 
    { 
 
     get 
 
     { 
 
      if (ExtensionGet("wWWHomePage").Length != 1) 
 
       return null; 
 

 
      return (string)ExtensionGet("wWWHomePage")[0]; 
 

 
     } 
 
     set { this.ExtensionSet("wWWHomePage", value); } 
 
    }

로하지만 TermSrvProfilePath와 함께 다른 한 느낌과 시도

은 ...

+1

이 질문에 대한 답을 제공하지 않습니다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남기십시오. 자신의 게시물에 언제나 댓글을 달 수 있으며 충분한 [평판] (http://stackoverflow.com/help/whats-reputation)을 갖게되면 [모든 게시물에 댓글을 달 수] 있어야합니다 (http://stackoverflow.com/help/privileges/comment). –

+0

나는 그의 마지막 답글을 언급 할 수 없다 :/나는 평판이 충분하지 않다. –

관련 문제