6
A
回答
4
您可以使用System.DirectoryServices命名空間執行此操作。
Dim entry As DirectoryServices.DirectoryEntry
Dim mySearcher As System.DirectoryServices.DirectorySearcher
Dim result As System.DirectoryServices.SearchResult
Dim myEntry As DirectoryEntry
Dim domainName As String
Dim userId As String
Dim objectGuid As Guid
'Split the username into domain and userid parts
domainName = Page.User.Identity.Name.Substring(0, Page.User.Identity.Name.IndexOf("\"))
userId = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1)
'Start at the top level domain
entry = New DirectoryEntry(domainName)
mySearcher = New DirectorySearcher(entry)
'Build a filter for just the user
mySearcher.Filter = ("(&(anr=" & userId & ")(objectClass=user))")
'Get the search result ...
result = mySearcher.FindOne
'... and then get the AD entry that goes with it
myEntry = result.GetDirectoryEntry
'The Guid property is the objectGuid
objectGuid = myEntry.Guid
有可能是一個更好的方法來做到這一點,但這是有效的!
2
您需要使用NativeGuid屬性。 C#代碼:
string login = HttpContext.Current.User.Identity.Name;
string domain = login.Substring(0, login.IndexOf('\\'));
string userName = login.Substring(login.IndexOf('\\') + 1);
DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + domain);
DirectorySearcher searcher = new DirectorySearcher(domainEntry);
searcher.Filter = string.Format(
"(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))",
userName);
SearchResult searchResult = searcher.FindOne();
DirectoryEntry entry = searchResult.GetDirectoryEntry();
Guid objectGuid = new Guid(entry.NativeGuid);
11
建議的解決方案相當昂貴。而不是由域和用戶名搜索,更好的解決方案是使用SID來查找帳戶:
// using System.Security.Principal;
IPrincipal userPrincipal = HttpContext.Current.User;
WindowsIdentity windowsId = userPrincipal.Identity as WindowsIdentity;
if (windowsId != null)
{
SecurityIdentifier sid = windowsId.User;
using(DirectoryEntry userDe = new DirectoryEntry("LDAP://<SID=" + sid.Value + ">"))
{
Guid objectGuid = new Guid(userDe.NativeGuid);
}
}
相關問題
- 1. AD用戶身份驗證
- 2. 用asp.net進行Azure AD身份驗證身份驗證
- 3. 如何從ASP.NET網頁獲取SharePoint身份驗證用戶?
- 4. ICINGA的AD身份驗證
- 5. 需要限制AD用戶 - ASP.NET中的Windows身份驗證
- 6. 從Azure AD v1身份驗證轉換爲Azure AD v2身份驗證
- 7. 從.Net Web API中的Azure經過身份驗證的AD獲取用戶名
- 8. 試圖根據AD對用戶進行身份驗證 - ASP.NET MVC
- 9. Sharepoint Azure AD身份驗證
- 10. AD FS身份驗證
- 11. WCF - 傳輸身份驗證 - 獲取身份驗證用戶的憑證
- 12. 獲取ASP.NET中的用戶ID身份驗證
- 13. 獲取用戶的身份而不進行身份驗證
- 14. 本地AD到ASP.NET Core的Azure AD身份驗證/授權
- 15. Tomcat AD身份驗證 - 允許所有AD用戶
- 16. ASP.NET中的用戶身份驗證
- 17. Azure AD身份驗證錯誤:用戶已取消流程
- 18. 使用Windows身份驗證和匿名身份驗證獲取用戶名
- 19. Azure AD身份驗證以及Passport身份驗證
- 20. Azure AD B2C - 身份驗證挑戰不觸發身份驗證
- 21. ASP.NET窗體身份驗證 - 獲取用戶ID
- 22. ASP.NET窗體身份驗證 - 獲取用戶數據
- 23. 第三方AD租戶的Azure AD身份驗證
- 24. 在ASP .NET Core的Windows身份驗證中爲用戶獲取AD信息
- 25. ASP.NET MVC用戶身份驗證
- 26. ASP.NET IIS用戶角色身份驗證
- 27. Asp.net用戶身份驗證和授權
- 28. asp.net身份驗證和用戶數據
- 29. Azure AD身份驗證體驗流程
- 30. 使用表單和AD身份驗證?
它看起來正確的,我就給你明天一試。謝謝。 – 2008-11-25 15:36:01
謝謝。爲了得到正確的objectGuid,我使用下面的代碼: objectGuid = System.Guid.Parse(myEntry.NativeGuid) – geekinit 2012-08-06 13:45:06