Vì bạn đang ở trên một mạng lưới cửa sổ, sau đó bạn cần phải truy vấn Active directory để tìm kiếm cho người dùng và sau đó nhận được thuộc tính của nó chẳng hạn như email
Dưới đây là một ví dụ chức năng DisplayUser
rằng đưa ra một IIdentity
trên một cửa sổ xác nhận mạng, tìm email
của người dùng:
public static void Main() {
DisplayUser(WindowsIdentity.GetCurrent());
Console.ReadKey();
}
public static void DisplayUser(IIdentity id) {
WindowsIdentity winId = id as WindowsIdentity;
if (id == null) {
Console.WriteLine("Identity is not a windows identity");
return;
}
string userInQuestion = winId.Name.Split('\\')[1];
string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in
// the account that this program runs in should be authenticated in there
DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);
DirectorySearcher adSearcher = new DirectorySearcher(entry);
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
SearchResult userObject = adSearcher.FindOne();
if (userObject != null) {
string[] props = new string[] { "title", "mail" };
foreach (string prop in props) {
Console.WriteLine("{0} : {1}", prop, userObject.Properties[prop][0]);
}
}
}
cho này:
Edit: Nếu bạn nhận được 'người dùng xấu/lỗi mật khẩu' Tài khoản rằng mã chạy dưới phải có quyền truy cập cho người dùng miền. Nếu bạn chạy mã trong asp.net thì ứng dụng web phải được chạy dưới một hồ bơi ứng dụng với thông tin đăng nhập với quyền truy cập miền. Xem here để biết thêm thông tin
Nguồn
2010-10-21 10:27:12
Bạn đang sử dụng nhà cung cấp memebership trong ứng dụng của bạn? – Restuta
không, ứng dụng của tôi. là trên mạng nội bộ để sử dụng xác thực cửa sổ. –