Windows – Win32:如何验证Active Directory的凭据?
它已经是
asked和
answered for .NET,但现在是时候得到本机Win32代码的答案:
如何验证Windows用户名和密码? 我asked this question before for managed code.现在是时候为本地解决方案. 需要指出一些更常见的解决方案的陷阱: 方法无效1.使用模拟查询Active Directory 很多人suggest querying the Active Directory的东西.如果抛出异常,那么您知道凭据无效 – 如this stackoverflow question所述. 但是有some serious drawbacks to this approach:
使用 无效方法1a – .NET DirectoryEntry entry = new DirectoryEntry("persuis","iboyd","Tr0ub4dor&3"); object nativeObject = entry.NativeObject; 无效方法1b – .NET#2 public static Boolean CheckADUserCredentials(String accountName,String password,String domain) { Boolean result; using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,accountName,password)) { using (DirectorySearcher searcher = new DirectorySearcher(entry)) { String filter = String.Format("(&(objectCategory=user)(sAMAccountName={0}))",accountName); searcher.Filter = filter; try { SearchResult adsSearchResult = searcher.FindOne(); result = true; } catch (DirectoryServicesCOMException ex) { const int SEC_E_LOGON_DENIED = -2146893044; //0x8009030C; if (ex.ExtendedError == SEC_E_LOGON_DENIED) { // Failed to authenticate. result = false; } else { throw; } } } } 以及通过ADO连接查询Active Directory: 方法1c无效 – 本地查询 connectionString = "Provider=ADsDSOObject; User ID=iboyd;Password=Tr0ub4dor&3; Encrypt Password=True;Mode=Read; Bind Flags=0;ADSI Flag=-2147483648';" SELECT userAccountControl FROM 'LDAP://persuis/DC=stackoverflow,DC=com' WHERE objectClass='user' and sAMAccountName = 'iboyd' 即使您的凭据有效,但您没有权限查看您的目录条目,这些都会失败: 方法无效2. LogonUser Win32 API Others建议使用LogonUser() API函数.这听起来不错,但不幸的是,主叫用户有时需要通过授权给操作系统本身:
作为微软在knowledge base article中指出的那样,将“作为操作系统的一部分”作为优势并不是你想做的事情 –
另外,如果指定了空白密码,对LogonUser()的调用将失败. 有效的.NET 3.5方法 – PrincipalContext 有一种验证方法,仅在.NET 3.5和更高版本中可用,允许用户进行身份验证,而无需执行授权检查: // create a "principal context" - e.g. your domain (could be machine,too) using(PrincipalContext pc = new PrincipalContext(ContextType.Domain,"stackoverflow.com")) { // validate the credentials bool isValid = pc.ValidateCredentials("iboyd","Tr0ub4dor&3") } 不幸的是,此代码仅在.NET 3.5及更高版本中可用. 现在是找到本机等价物的时候了.
Here is Microsoft’s recommendation.
对于其他答案,我不太确定你为什么要拍摄他们.您尝试验证凭据时抱怨(相对边缘的情况)失败,但是如果要使用这些凭据实际执行某些操作,那么该操作将会失败.如果你不会真正用这些凭证做某事,那么为什么要首先验证它们呢?这似乎是一个有点设计的情况,但显然我不知道你想要完成什么. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-xp – 如何为Windows XP的批处理文件设置处理器关联
- 在Windows上运行sudo apt-get命令
- active-directory – AD CS:证书模板不可用
- 良好的开源错误跟踪/问题跟踪sofware for Windows
- 开发了一款写作软件(OSX,Windows),附带Electron开发指南
- windows – 技术上是否可以将TFS 2010升级到TFS 2013 Expre
- windows – DNS – 从旧服务器重定向到新服务器
- windows-server-2012-r2 – 如何从主DC断开备用DC?
- 完全复制Windows中的应用程序安装
- mssql下调用dll