且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 12:26:10

您可以使用 System.DirectoryServices 命名空间执行此操作.

You can do this with the System.DirectoryServices namespace.

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

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

There might be a better way to do this, but this works!