久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將成員從受信任的域添加到 AD 組

Add member to AD group from a trusted domain(將成員從受信任的域添加到 AD 組)
本文介紹了將成員從受信任的域添加到 AD 組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有兩個域,處于信任關系中,我試圖從 C# Web 應用程序管理它們.為此,我必須模擬兩個不同的技術用戶,但效果很好,所以我不會強調那部分代碼.

I have two domains, in a trusted relationship, that I'm trying to manage from a C# web application. To do that, I have to impersonate two different technical users, but that works good, so I will not emphasize that part of the code.

要為文件系統構建適當且易于管理的 ACL,我必須

To build proper and easy to manage ACLs for the file system, I must

  • 在域 A 中創建一個組(好的!)
  • 在 domainB 中查找用戶(好的!)
  • 將用戶添加到組中(提交更改時失敗,錯誤消息:服務器上沒有此類對象.(來自 HRESULT 的異常:0x80072030))

如果我添加來自同一個域的用戶,則代碼運行良好,所以我相信我在這里只遺漏了一小部分信息.我使用本文檔作為參考看到了這個問題 以及(還有一些引用此錯誤消息),但它們都沒有幫助.

If I'm adding a user from the same domain, the code works perfectly, so I believe I'm only missing a small partial info here. I used this document as a reference and saw this question as well (and a few more citing this error message) but neither of them helped.

代碼(刪除了try-catch塊以使其更簡單)

Code (try-catch block removed to make it simpler)

// de is a DirectoryEntry object of the AD group, received by the method as a parameter
// first impersonation to search in domainB
// works all right
if (impersonator.impersonateUser("techUser1", "domainB", "pass")) {
    DirectoryEntry dom = new DirectoryEntry("LDAP://domainB.company.com/OU=MyOU,DC=domainB,DC=company,DC=com", "techUser1", "pass");
    de.Invoke("Add", new object[] { "LDAP://domainB.company.com/CN=theUserIWantToAdd,OU=MyOU,DC=domainB,DC=company,DC=com" });
    // de.Invoke("Add", new object[] { "LDAP://domainA.company.com/CN=anotherUserFromDomainA,OU=AnotherOU,DC=domainB,DC=company,DC=com" });
    impersonator.undoImpersonation();
}

// second impersonation because the group (de) is in domainA
// and techUser2 has account operator privileges there
if (impersonator.impersonateUser("techUser2", "domainA", "pass"))
{
    de.CommitChanges();
    impersonator.undoImpersonation();
    return true;
}
else
{
    // second impersonation was unsuccessful, so return an empty object
    return false;
}

第 6 行有效,如果我調試它或強制將屬性寫入 HttpResponse,它顯然就在那里.所以 LDAP 查詢似乎沒問題.

Line 6 works, if I debug it or force the properties to be written to HttpResponse, it is clearly there. So the LDAP queries seem to be OK.

此外,如果我注釋掉第 6 行并取消注釋第 7 行,那么基本上我添加了一個來自同一域的用戶,整個事情會奇跡般地運行.對于域B,我被卡住了.有什么好的建議嗎?

Also, if I comment out line 6 and uncomment 7, so basically I add a user from the same domain, the whole thing works miraculously. With domainB, I'm stuck. Any good piece of advice?

推薦答案

按照你的代碼,我看到你得到 de 作為參數,它在 Domain A.然后你正在創建 DirectoryEntry 對象 dom,它被 模擬,但從未被使用過.但是,您正在嘗試使用 LDAP 直接將對象從 Domain B 添加到 de.這一行:

Following your code, I see that you're getting de as a parameter, which is in Domain A. Then you're creating DirectoryEntry object dom, which is getting impersonated, but never getting used. However, you're trying to add an object from Domain B to de directly using LDAP. This line:

de.Invoke("Add", new object[{"LDAP://domainB.company.com/CN=theUserIWantToAdd,OU=MyOU,DC=domainB,DC=company,DC=com" }); 

沒有被模擬.

假設您的 impersonation 工作正常, 使用 dom 對象,該對象已經 impersonatedDirectorySearcherDomain B中找到用戶,然后將Domain B中的用戶對象添加到de.

Assuming your impersonation works correctly, use dom object which is already impersonated with DirectorySearcher to find the user in Domain B and then add the user object from Domain B to de.

...
using (DirectoryEntry dom = new DirectoryEntry("LDAP://domainB.company.com/OU=MyOU,DC=domainB,DC=company,DC=com", "techUser1", "pass"))
{
    using (DirectorySearcher searcher = new DirectorySearcher(dom))
    {
        searcher.Filter = "(&(objectClass=user)(CN=theUserIWantToAdd))";
        SearchResult result = searcher.FindOne();
        de.Invoke("Add", new object[] { result.Path });
    }
}
...

UDPATE

此示例將向您展示如何從一個域中獲取用戶 SID、從另一個域中搜索組并使用 SID 將用戶添加到組中.

UDPATE

This example will show you how to get user SID from one domain, search group from another domain and add user to group using SID.

//GET THE USER FROM DOMAIN B
using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domainContext, UPN))
{
    if (userPrincipal != null)
    {
       //FIND THE GROUP IN DOMAIN A
       using (GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, groupName))
       {
          if (groupPrincipal != null)
          {
             //CHECK TO MAKE SURE USER IS NOT IN THAT GROUP
             if (!userPrincipal.IsMemberOf(groupPrincipal))
             {
                string userSid = string.Format("<SID={0}>", userPrincipal.SID.ToString());
                DirectoryEntry groupDirectoryEntry = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
                groupDirectoryEntry.Properties["member"].Add(userSid);
                groupDirectoryEntry.CommitChanges();
              }
           }
        }
     }
 }

請注意,我跳過了上面代碼中的所有impersonation.

Please note that I skipped all the impersonation in the above code.

這篇關于將成員從受信任的域添加到 AD 組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 久久99精品久久久久久噜噜 | 日本精品视频一区二区三区四区 | 日韩一级在线 | 国产欧美视频一区二区 | 涩涩99| 国产精品国产a级 | 久久黄色精品视频 | 日韩a视频 | 男人的天堂在线视频 | 久久精品99久久 | 日本不卡一区 | 日日天天 | 夜夜爽99久久国产综合精品女不卡 | 一区二区三区免费 | 亚洲精品福利在线 | 欧美日韩一 | 欧美区日韩区 | 国产亚洲一区二区三区 | 亚洲精品自在在线观看 | 国产在线一区二区三区 | 久久久免费电影 | 日韩一区二区三区在线看 | 福利一区二区在线 | 久草中文在线观看 | 欧美精品一 | 久久久久久国产一区二区三区 | 91久久国产综合久久91精品网站 | 99tv| 殴美成人在线视频 | 精品日韩在线 | 亚洲一级淫片 | 韩国电影久久 | 日韩国产精品一区二区三区 | 亚洲一区av| 亚洲欧美日韩在线一区二区 | 中文字幕的av | 91在线精品一区二区 | 国产在线一区二 | 毛片一级电影 | 成人午夜视频在线观看 | 黄视频欧美|