Tôi đang sử dụng xác thực cửa sổ trong một ứng dụng ASP.NET. Tôi tự hỏi làm thế nào để tốt nhất có được các objectGuid từ người dùng hiện đang đăng nhập?Nhận xác thực người dùng AD objectGuid từ asp.net
Kính trọng, Egil.
Tôi đang sử dụng xác thực cửa sổ trong một ứng dụng ASP.NET. Tôi tự hỏi làm thế nào để tốt nhất có được các objectGuid từ người dùng hiện đang đăng nhập?Nhận xác thực người dùng AD objectGuid từ asp.net
Kính trọng, Egil.
Bạn có thể thực hiện việc này với không gian tên 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
Có thể có cách tốt hơn để thực hiện việc này, nhưng điều này có hiệu quả!
Bạn cần sử dụng tài sản NativeGuid. C# code:
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);
Giải pháp đề xuất khá đắt. Thay vì tìm kiếm theo tên miền và tên người dùng, giải pháp tốt hơn là sử dụng SID để tra cứu tài khoản:
// 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);
}
}
Nếu windowsId là null thì sao? – dpp
Sau đó, có khả năng bạn đang giao dịch với một người dùng ẩn danh hoặc một người không tin tưởng trang web để cho phép xác thực Kerberos. Bạn có thể làm việc với các quản trị viên trong công ty của bạn để thúc đẩy chính sách nhóm để tin tưởng trang * nội bộ *. Hoặc bạn có thể cung cấp một mẫu đăng nhập https hoặc một số phương pháp xác thực khác. – Felan
Có vẻ đúng, tôi sẽ thử vào ngày mai. Cảm ơn. –
Cảm ơn bạn. Để có được objectGuid chính xác, tôi đã sử dụng mã này thay thế: objectGuid = System.Guid.Parse (myEntry.NativeGuid) – geekinit