从asp.net获取验证AD用户objectGuid

前端之家收集整理的这篇文章主要介绍了从asp.net获取验证AD用户objectGuid前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在ASP.NET应用程序中使用Windows身份验证.我想知道如何最好地从当前登录用户获取objectGuid?

此致,埃吉尔.

解决方法

您可以使用System.DirectoryServices命名空间执行此操作.
  1. Dim entry As DirectoryServices.DirectoryEntry
  2. Dim mySearcher As System.DirectoryServices.DirectorySearcher
  3. Dim result As System.DirectoryServices.SearchResult
  4. Dim myEntry As DirectoryEntry
  5. Dim domainName As String
  6. Dim userId As String
  7. Dim objectGuid As Guid
  8.  
  9. 'Split the username into domain and userid parts
  10. domainName = Page.User.Identity.Name.Substring(0,Page.User.Identity.Name.IndexOf("\"))
  11. userId = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1)
  12.  
  13. 'Start at the top level domain
  14. entry = New DirectoryEntry(domainName)
  15.  
  16. mySearcher = New DirectorySearcher(entry)
  17.  
  18. 'Build a filter for just the user
  19. mySearcher.Filter = ("(&(anr=" & userId & ")(objectClass=user))")
  20.  
  21. 'Get the search result ...
  22. result = mySearcher.FindOne
  23.  
  24. '... and then get the AD entry that goes with it
  25. myEntry = result.GetDirectoryEntry
  26.  
  27. 'The Guid property is the objectGuid
  28. objectGuid = myEntry.Guid

可能有更好的方法来做到这一点,但这是有效的!

猜你在找的asp.Net相关文章