域服務數據讀寫,有倆種模式
1、輕量級的數據讀取
_domain是服務器的域名
獲取連接PrincipalContext pc = new PrincipalContext(ContextType.Domain, _domain)
驗證賬號密碼pc.ValidateCredentials(jobNumber, password)
引用System.DirectoryServices.AccountManagement命名空間後,按照如下如下方法,AD域驗證
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, _domain)) { // 工號一定要全 using (var userPrincipal = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName,loginModel.JobNumber)) { if (userPrincipal == null) { return "賬號不正確,請重新輸入"; } if (!pc.ValidateCredentials(loginModel.JobNumber, loginModel.Password)) { return @"密碼輸入錯誤,請重新輸入"; } //GivenName是用戶名稱,Surname是工號(無前綴),Name是用戶名稱+工號(無前綴) PersonDetailInfo personDetailInfo = new PersonDetailInfo() { SearchName = userPrincipal.Name, UserName = userPrincipal.GivenName, JobNumber = userPrincipal.SamAccountName, EmailAddress = userPrincipal.EmailAddress }; return personDetailInfo; } }
2、DectoryEntry
可以獲取整個服務器的數據,也可以修改其中的信息
參考類似文章:
http://www.it165.net/pro/html/201308/6829.html