ValidateCredentials始终返回false [英] ValidateCredentials always returns false
问题描述
我想在应用程序上使用此方法,以便用户通过输入其Windows网络用户ID和密码登录:
Imports System.DirectoryServices.AccountManagement
Public 函数 IsValid( ByVal UserId As 字符串, ByVal 密码作为 String ) as Boolean
Dim 结果作为 布尔 = 错误
尝试
昏暗 PC As 新 PrincipalContext(ContextType.Domain, MyDomain)
结果= PC.ValidateCredentials(UserId,密码)
Catch ex As 异常
投掷 ex
结束 尝试
返回结果
< span class =code-keyword>结束 功能
我发现的每一份文件都证明这是正确的。我已经确认正在正确创建 PC
并指向正确的网络域。我已经验证用户标识和密码都是正确的,并且用户存在于请求的域中。然而,对 PC.ValidateCredentials
的调用总是返回false。
我有另一个方法,它通过ActiveDirectory,但它涉及很多开销,大约需要10秒钟来验证。那个用相同的域名,用户ID和密码返回true。
我假设我做错了什么。有什么建议?
您好,
您可以尝试以下代码。
使用pc = New PrincipalContext(ContextType.Domain, domain.lan,用户名,密码)
如果pc.ValidateCredentials(用户名,密码)那么
尝试
使用searcher = New PrincipalSearcher(New UserPrincipal(pc))
searcher.QueryFilter .SamAccountName = username
Dim u As Principal = searcher.FindOne()
End using
Catch generatedExceptionName As Exception
返回 没有权利处理广告
结束尝试
否则
返回 用户无法登录
结束如果
结束使用
希望这对你有帮助。
谢谢
喜欢这个:
Imports System.DirectoryServices.AccountManagement
Public < span class =code-keyword>函数 IsValid( ByVal UserId 作为 字符串, ByVal 密码作为 String ) As Boolean
Dim 结果作为 布尔
尝试
结果= 错误
Dim PC 作为 新 PrincipalContext(ContextType.Domain, MyDomain)
结果= PC.ValidateCredentials(UserId,密码)
Catch ex 作为例外
投掷 ex
结束 < span class =code-keyword>尝试
返回结果
结束 功能
引用:最终解决方案由Gurvinder Singh @ iGate
使用(PrincipalContext pc = new PrincipalContext(ContextType.Domain,serverName))
{
return pc.ValidateCredentials(userName,password,ContextOptions.Negotiate);
}
这对我来说总是有用
ContextOptions.Negotiate
I would like to use this method on an application, so that a user would log in by entering their Windows network user id and password:
Imports System.DirectoryServices.AccountManagement
Public Function IsValid(ByVal UserId As String, ByVal Password As String) As Boolean
Dim Result As Boolean = False
Try
Dim PC As New PrincipalContext(ContextType.Domain, "MyDomain")
Result = PC.ValidateCredentials(UserId, Password)
Catch ex As Exception
Throw ex
End Try
Return Result
End Function
Every piece of documentation I have found shows this to be correct. I have verified that PC
is being created properly and is pointing to the correct network domain. I have verified that the userid and password are both correct, and that the user exists in the requested domain. And yet, the call to PC.ValidateCredentials
always returns false.
I have a another method, which goes through ActiveDirectory, but it involves a lot of overhead and takes about 10 seconds to validate. That one returns true with the same domain, userid and password.
I assume I'm doing something wrong. Any suggestions?
Hi,
You can try following code.
Using pc = New PrincipalContext(ContextType.Domain, "domain.lan", username, password) If pc.ValidateCredentials(username, password) Then Try Using searcher = New PrincipalSearcher(New UserPrincipal(pc)) searcher.QueryFilter.SamAccountName = username Dim u As Principal = searcher.FindOne() End Using Catch generatedExceptionName As Exception Return "no rights to work on ad" End Try Else Return "user cannot login" End If End Using
Hope this will help you.
thanks
Like This:
Imports System.DirectoryServices.AccountManagement Public Function IsValid(ByVal UserId As String, ByVal Password As String) As Boolean Dim Result As Boolean Try Result = False Dim PC As New PrincipalContext(ContextType.Domain, "MyDomain") Result = PC.ValidateCredentials(UserId, Password) Catch ex As Exception Throw ex End Try Return Result End Function
Quote:Final Solution By Gurvinder Singh @ iGate
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, serverName)) { return pc.ValidateCredentials(userName, password, ContextOptions.Negotiate); }
This always works for me
ContextOptions.Negotiate
这篇关于ValidateCredentials始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!