C#?網(wǎng)域賬號(hào)(Domain)驗(yàn)證的實(shí)現(xiàn)
使用C#對(duì)網(wǎng)域賬號(hào)(Domain)驗(yàn)證方案:
一、使用advapi32.dll動(dòng)態(tài)庫(kù)
[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; //通過(guò)網(wǎng)絡(luò)驗(yàn)證賬戶合法性 const int LOGON32_PROVIDER_DEFAULT = 0; //使用默認(rèn)的Windows 2000/NT NTLM驗(yàn)證方 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; }
注意使用該動(dòng)態(tài)庫(kù)可能會(huì)導(dǎo)致 服務(wù)Local Security Authority Process 內(nèi)存異常升高且無(wú)法回收現(xiàn)象
二、使用 System.DirectoryServices
/// <summary> /// 驗(yàn)證網(wǎng)域賬號(hào) /// </summary> /// <param name="account">賬號(hào)</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)//驗(yàn)證成功 { if (result.Properties["cn"] != null)//依據(jù)實(shí)際屬性獲取用戶信息 { name = result.Properties["cn"][0].ToString(); } return true; } return false; } catch { return false; } } }
注意如果域內(nèi)賬號(hào)較多時(shí),驗(yàn)證不存在的賬號(hào)速度較慢且不會(huì)驗(yàn)證密碼的有效期
三、使用System.DirectoryServices.AccountManagement
/// <summary> /// 驗(yàn)證網(wǎng)域賬號(hào) /// </summary> /// <param name="account">賬號(hào)</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; } } } }
注意該方法不會(huì)驗(yàn)證密碼的有效期
到此這篇關(guān)于C# 網(wǎng)域賬號(hào)(Domain)驗(yàn)證的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 網(wǎng)域賬號(hào)驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡(jiǎn)單實(shí)用,希望能幫到大家。2016-05-05c#數(shù)據(jù)綁定之將datatabel的data添加listView
這篇文章主要介紹了c#將DataTabel的data添加ListView的示例,實(shí)現(xiàn)功能是通過(guò)響應(yīng)UI Textbox 的值向ListView 綁定新添加的紀(jì)錄。 ,需要的朋友可以參考下2014-04-04c#發(fā)送請(qǐng)求訪問(wèn)外部接口的實(shí)例
這篇文章主要介紹了c#發(fā)送請(qǐng)求訪問(wèn)外部接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01