我想根據域控制器驗證一組憑據。例如:如何驗證域憑證(從本機代碼)?
Username: joel
Password: splotchy
Domain: STACKOVERFLOW
在.NET 3.5和更新的版本中you can use PrincipalContext.ValidateCredentials(username, password)
。
否則你有麻煩。
繼Microsoft知識庫文章How to validate user credentials on Microsoft operating systems在代碼中,我得到的地方,你打電話AcceptSecurityContext
點:
ss = AcceptSecurityContext(
@pAS._hcred, //[in]CredHandle structure
phContext, //[in,out]CtxtHandle structure
@InBuffDesc, //[in]SecBufferDesc structure
0, //[in]context requirement flags
SECURITY_NATIVE_DREP, //[in]target data representation
@pAS._hctxt, //[in,out]CtxtHandle strcture
@OutBuffDesc, //[in,out]SecBufferDesc structure
ContextAttributes, //[out]Context attribute flags
@Lifetime); //[out]Timestamp struture
除了函數失敗:
SEC_E_NO_AUTHENTICATING_AUTHORITY
(0x80090311 )該功能失敗。沒有權威可以聯繫認證。這可能是由於以下情況:
- 驗證方的域名不正確。
- 該域名不可用。
- 信任關係失敗。
這將是一個有用的錯誤,但我可以使用驗證從.NET 3.5相同的憑據:
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, domain))
{
valid = context.ValidateCredentials(username, password);
}
什麼可能會發生,讓.NET來驗證一組憑據,而本機代碼不能?
更新:LogonUser
也失敗:
LogonUser("[email protected]", null, "splotchy",
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_WINNT50, out token);
與
1311 - There are currently no logon servers available to service the logon request
更新兩個:我都試過首選Negotiate
提供商,以及作爲第四Ë的Windows NT4遺產「NTLM」提供
String package = "Negotiate"; //"NTLM"
QuerySecurityPackageInfo(package, [out] packageInfo);
...
AcquireCredentialsHandle(
null, //[in] principle
package, //[in] package
SECPKG_CRED_OUTBOUND, //[in] credential use
null, //[in] LogonID
pAuthIdentity, //[in] authData
null, //[in] GetKeyFn, not used and should be null
null, //[in] GetKeyArgument, not used and should be null
credHandle, //[out] CredHandle structure
expires); //[out] expiration TimeStamp structure
你是如何調用'InitializeSecurityContext'? (哪個SSP,特別是?)你如何設置CredHandle? – 2012-03-15 16:34:03
@EdwardThomson我試過優選'Negotiate'提供商,以及'NTLM'。 'CredHandle'通過調用'AcquireCredentialsHandle'來初始化。 – 2012-03-15 18:02:20
我似乎記得有一個類似的問題,雖然這是幾年前,這個問題是模糊的。如果您運行瓦特/提升權限,是否有任何更改? (域管理員也許?) – 2012-03-15 18:04:02