C#?網(wǎng)域賬號(Domain)驗證的實現(xiàn)
更新時間:2024年04月18日 09:44:31 作者:YoungerXu
本文主要介紹了C#?網(wǎng)域賬號(Domain)驗證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
使用C#對網(wǎng)域賬號(Domain)驗證方案:
一、使用advapi32.dll動態(tài)庫
[DllImport("advapi32.dll")] private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); const int LOGON32_LOGON_INTERACTIVE = 2; //通過網(wǎng)絡驗證賬戶合法性 const int LOGON32_PROVIDER_DEFAULT = 0; //使用默認的Windows 2000/NT NTLM驗證方 public static bool CheckADAccount(string account, string password) { IntPtr tokenHandle = new IntPtr(0); tokenHandle = IntPtr.Zero; string domainName = "dpbg"; if (LogonUser(account, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle)) return true; return false; }
注意使用該動態(tài)庫可能會導致 服務Local Security Authority Process 內(nèi)存異常升高且無法回收現(xiàn)象
二、使用 System.DirectoryServices
/// <summary> /// 驗證網(wǎng)域賬號 /// </summary> /// <param name="account">賬號</param> /// <param name="password">密碼</param> /// <param name="domain">網(wǎng)域</param> /// <param name="name">姓名</param> /// <returns></returns> public static bool CheckADAccountNew(string account, string password, string domain, out string name) { name = ""; using (DirectoryEntry deUser = new DirectoryEntry(@"LDAP://" + domain, account, password)) { DirectorySearcher src = new DirectorySearcher(deUser); src.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + account + "))"; src.PropertiesToLoad.Add("cn"); src.SearchRoot = deUser; src.SearchScope = SearchScope.Subtree; try { SearchResult result = src.FindOne(); if (result != null)//驗證成功 { if (result.Properties["cn"] != null)//依據(jù)實際屬性獲取用戶信息 { name = result.Properties["cn"][0].ToString(); } return true; } return false; } catch { return false; } } }
注意如果域內(nèi)賬號較多時,驗證不存在的賬號速度較慢且不會驗證密碼的有效期
三、使用System.DirectoryServices.AccountManagement
/// <summary> /// 驗證網(wǎng)域賬號 /// </summary> /// <param name="account">賬號</param> /// <param name="password">密碼</param> /// <param name="domain">網(wǎng)域</param> /// <param name="name">姓名</param> /// <returns></returns> public static bool CheckADAccountNew(string account, string password, string domain, out string name) { name = ""; using (var domainContext = new PrincipalContext(ContextType.Domain, domain)) { using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, account)) { if (foundUser == null) { return false; } name = foundUser.Name; if (domainContext.ValidateCredentials(account, password)) { return true; } else { return false; } } } }
注意該方法不會驗證密碼的有效期
到此這篇關于C# 網(wǎng)域賬號(Domain)驗證的實現(xiàn)的文章就介紹到這了,更多相關C# 網(wǎng)域賬號驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡單實用,希望能幫到大家。2016-05-05c#數(shù)據(jù)綁定之將datatabel的data添加listView
這篇文章主要介紹了c#將DataTabel的data添加ListView的示例,實現(xiàn)功能是通過響應UI Textbox 的值向ListView 綁定新添加的紀錄。 ,需要的朋友可以參考下2014-04-04