2016-05-14 97 views
0

我正在使用打擊代碼在本地計算機(虛擬桌面)客戶端的AD LDS中創建帳戶/用戶。 在我本地的下面的代碼中工作正常但是在將代碼部署到與安裝AD LDS不同的其他服務器之後,它會引發錯誤「服務器上沒有這樣的對象」完整的響應標題如下。AD LDS錯誤「服務器上沒有這樣的對象」

Var host = "Hostname";// soemthing like SV1DCVDEVDB789 where AD LDS is instaed 
var port = 389;//Port Number 
var machineName = string.Format("{0}:{1}", host, port); 
var container = "CN=PSExtUser,CN=PSADLDSPartition1,DC=PS,DC=COM"; 
var principalContext = new PrincipalContext(ContextType.ApplicationDirectory, machineName, container); 

//Check id user already exist 

UserPrincipal usr = UserPrincipal.FindByIdentity(principalContext, userId); 


//if usr is null create new user as below code 

     UserPrincipal newUser = new UserPrincipal(principalContext); 
       newUser.Name = userId; 
       newUser.UserPrincipalName = userId; 
       newUser.SetPassword(pwd.ToString()); 
       newUser.Enabled = false; 
       newUser.Save(); 



////REsponse obtained 





[DirectoryServicesCOMException (0x80072030): There is no such object on the server. 
] 
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +597561 
    System.DirectoryServices.DirectoryEntry.Bind() +44 
    System.DirectoryServices.DirectoryEntry.get_AdsObject() +42 
    System.DirectoryServices.DirectoryEntry.get_Options() +42 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() +351 

[PrincipalOperationException: There is no such object on the server. 
] 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() +495548 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoApplicationDirectoryInit() +61 
    System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +184 
    System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42 

回答

1

我知道這個問題很舊,所以你可能已經找到了解決辦法。但對於通過Google來到的其他人:

如果您告訴我們哪一行正在拋出錯誤,但我猜測它是SetPassword有幫助。設置該密碼需要該帳戶已存在。因此請將您的SetPassword移至Save()之後。我相信Enabled也是如此。

UserPrincipal newUser = new UserPrincipal(principalContext); 
      newUser.Name = userId; 
      newUser.UserPrincipalName = userId; 
      newUser.Save(); 

      newUser.Enabled = false; 
      newUser.SetPassword(pwd.ToString()); 
      newUser.Save();