sample code the following visual basic module contains a public function called sspvalidateuser(). this function attempts to validate the supplied user name, domain name, and password by using sspi functions.
type secpkginfo fcapabilities as long wversion as integer wrpcid as integer cbmaxtoken as long name as long comment as long end type
type sechandle dwlower as long dwupper as long end type
type auth_seq finitialized as boolean fhavecredhandle as boolean fhavectxthandle as boolean hcred as sechandle hctxt as sechandle end type
type sec_winnt_auth_identity user as string userlength as long domain as string domainlength as long password as string passwordlength as long flags as long end type
type timestamp lowpart as long highpart as long end type
type secbuffer cbbuffer as long buffertype as long pvbuffer as long end type
type secbufferdesc ulversion as long cbuffers as long pbuffers as long end type
public type osversioninfo dwosversioninfosize as long dwmajorversion as long dwminorversion as long dwbuildnumber as long dwplatformid as long szcsdversion as string * 128 end type
private declare sub copymemory lib "kernel32" alias "rtlmovememory" _ (destination as any, source as any, byval length as long)
private declare function nt4querysecuritypackageinfo lib "security" _ alias "querysecuritypackageinfoa" (byval packagename as string, _ byref ppackageinfo as long) as long
private declare function querysecuritypackageinfo lib "secur32" _ alias "querysecuritypackageinfoa" (byval packagename as string, _ byref ppackageinfo as long) as long
private declare function nt4freecontextbuffer lib "security" _ alias "freecontextbuffer" (byval pvcontextbuffer as long) as long
private declare function freecontextbuffer lib "secur32" _ (byval pvcontextbuffer as long) as long
private declare function nt4initializesecuritycontext lib "security" _ alias "initializesecuritycontexta" _ (byref phcredential as sechandle, byref phcontext as sechandle, _ byval psztargetname as long, byval fcontextreq as long, _ byval reserved1 as long, byval targetdatarep as long, _ byref pinput as secbufferdesc, byval reserved2 as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function initializesecuritycontext lib "secur32" _ alias "initializesecuritycontexta" _ (byref phcredential as sechandle, byref phcontext as sechandle, _ byval psztargetname as long, byval fcontextreq as long, _ byval reserved1 as long, byval targetdatarep as long, _ byref pinput as secbufferdesc, byval reserved2 as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function nt4initializesecuritycontext2 lib "security" _ alias "initializesecuritycontexta" _ (byref phcredential as sechandle, byval phcontext as long, _ byval psztargetname as long, byval fcontextreq as long, _ byval reserved1 as long, byval targetdatarep as long, _ byval pinput as long, byval reserved2 as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function initializesecuritycontext2 lib "secur32" _ alias "initializesecuritycontexta" _ (byref phcredential as sechandle, byval phcontext as long, _ byval psztargetname as long, byval fcontextreq as long, _ byval reserved1 as long, byval targetdatarep as long, _ byval pinput as long, byval reserved2 as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function nt4acquirecredentialshandle lib "security" _ alias "acquirecredentialshandlea" (byval pszprincipal as long, _ byval pszpackage as string, byval fcredentialuse as long, _ byval pvlogonid as long, _ byref pauthdata as sec_winnt_auth_identity, _ byval pgetkeyfn as long, byval pvgetkeyargument as long, _ byref phcredential as sechandle, byref ptsexpiry as timestamp) _ as long
private declare function acquirecredentialshandle lib "secur32" _ alias "acquirecredentialshandlea" (byval pszprincipal as long, _ byval pszpackage as string, byval fcredentialuse as long, _ byval pvlogonid as long, _ byref pauthdata as sec_winnt_auth_identity, _ byval pgetkeyfn as long, byval pvgetkeyargument as long, _ byref phcredential as sechandle, byref ptsexpiry as timestamp) _ as long
private declare function nt4acquirecredentialshandle2 lib "security" _ alias "acquirecredentialshandlea" (byval pszprincipal as long, _ byval pszpackage as string, byval fcredentialuse as long, _ byval pvlogonid as long, byval pauthdata as long, _ byval pgetkeyfn as long, byval pvgetkeyargument as long, _ byref phcredential as sechandle, byref ptsexpiry as timestamp) _ as long
private declare function acquirecredentialshandle2 lib "secur32" _ alias "acquirecredentialshandlea" (byval pszprincipal as long, _ byval pszpackage as string, byval fcredentialuse as long, _ byval pvlogonid as long, byval pauthdata as long, _ byval pgetkeyfn as long, byval pvgetkeyargument as long, _ byref phcredential as sechandle, byref ptsexpiry as timestamp) _ as long
private declare function nt4acceptsecuritycontext lib "security" _ alias "acceptsecuritycontext" (byref phcredential as sechandle, _ byref phcontext as sechandle, byref pinput as secbufferdesc, _ byval fcontextreq as long, byval targetdatarep as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function acceptsecuritycontext lib "secur32" _ (byref phcredential as sechandle, _ byref phcontext as sechandle, byref pinput as secbufferdesc, _ byval fcontextreq as long, byval targetdatarep as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function nt4acceptsecuritycontext2 lib "security" _ alias "acceptsecuritycontext" (byref phcredential as sechandle, _ byval phcontext as long, byref pinput as secbufferdesc, _ byval fcontextreq as long, byval targetdatarep as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function acceptsecuritycontext2 lib "secur32" _ alias "acceptsecuritycontext" (byref phcredential as sechandle, _ byval phcontext as long, byref pinput as secbufferdesc, _ byval fcontextreq as long, byval targetdatarep as long, _ byref phnewcontext as sechandle, byref poutput as secbufferdesc, _ byref pfcontextattr as long, byref ptsexpiry as timestamp) as long
private declare function nt4completeauthtoken lib "security" _ alias "completeauthtoken" (byref phcontext as sechandle, _ byref ptoken as secbufferdesc) as long
private declare function completeauthtoken lib "secur32" _ (byref phcontext as sechandle, _ byref ptoken as secbufferdesc) as long
private declare function nt4deletesecuritycontext lib "security" _ alias "deletesecuritycontext" (byref phcontext as sechandle) _ as long
private declare function deletesecuritycontext lib "secur32" _ (byref phcontext as sechandle) _ as long
private declare function nt4freecredentialshandle lib "security" _ alias "freecredentialshandle" (byref phcontext as sechandle) _ as long
private declare function freecredentialshandle lib "secur32" _ (byref phcontext as sechandle) _ as long
private declare function getprocessheap lib "kernel32" () as long
private declare function heapalloc lib "kernel32" _ (byval hheap as long, byval dwflags as long, _ byval dwbytes as long) as long
private declare function heapfree lib "kernel32" (byval hheap as long, _ byval dwflags as long, byval lpmem as long) as long
private declare function getversionexa lib "kernel32" _ (lpversioninformation as osversioninfo) as integer
dim g_nt4 as boolean
private function genclientcontext(byref authseq as auth_seq, _ byref authidentity as sec_winnt_auth_identity, _ byval pin as long, byval cbin as long, _ byval pout as long, byref cbout as long, _ byref fdone as boolean) as boolean
dim ss as long dim tsexpiry as timestamp dim sbdout as secbufferdesc dim sbout as secbuffer dim sbdin as secbufferdesc dim sbin as secbuffer dim fcontextattr as long
genclientcontext = false
if not authseq.finitialized then
if g_nt4 then ss = nt4acquirecredentialshandle(0&, "ntlm", _ secpkg_cred_outbound, 0&, authidentity, 0&, 0&, _ authseq.hcred, tsexpiry) else ss = acquirecredentialshandle(0&, "ntlm", _ secpkg_cred_outbound, 0&, authidentity, 0&, 0&, _ authseq.hcred, tsexpiry) end if
if not authseq.finitialized then authseq.finitialized = true end if
fdone = not (ss = sec_i_continue_needed _ or ss = sec_i_complete_and_continue)
genclientcontext = true
freeresourcesandexit:
if sbdout.pbuffers <> 0 then heapfree getprocessheap(), 0, sbdout.pbuffers end if
if sbdin.pbuffers <> 0 then heapfree getprocessheap(), 0, sbdin.pbuffers end if
end function
private function genservercontext(byref authseq as auth_seq, _ byval pin as long, byval cbin as long, _ byval pout as long, byref cbout as long, _ byref fdone as boolean) as boolean
dim ss as long dim tsexpiry as timestamp dim sbdout as secbufferdesc dim sbout as secbuffer dim sbdin as secbufferdesc dim sbin as secbuffer dim fcontextattr as long
genservercontext = false
if not authseq.finitialized then
if g_nt4 then ss = nt4acquirecredentialshandle2(0&, "ntlm", _ secpkg_cred_inbound, 0&, 0&, 0&, 0&, authseq.hcred, _ tsexpiry) else ss = acquirecredentialshandle2(0&, "ntlm", _ secpkg_cred_inbound, 0&, 0&, 0&, 0&, authseq.hcred, _ tsexpiry) end if
if not authseq.finitialized then authseq.finitialized = true end if
fdone = not (ss = sec_i_continue_needed _ or ss = sec_i_complete_and_continue)
genservercontext = true
freeresourcesandexit:
if sbdout.pbuffers <> 0 then heapfree getprocessheap(), 0, sbdout.pbuffers end if
if sbdin.pbuffers <> 0 then heapfree getprocessheap(), 0, sbdin.pbuffers end if
end function
public function sspvalidateuser(user as string, domain as string, _ password as string) as boolean
dim pspi as long dim spi as secpkginfo dim cbmaxtoken as long
dim pclientbuf as long dim pserverbuf as long
dim ai as sec_winnt_auth_identity
dim asclient as auth_seq dim asserver as auth_seq dim cbin as long dim cbout as long dim fdone as boolean
dim osinfo as osversioninfo
sspvalidateuser = false
' determine if system is windows nt (version 4.0 or earlier) osinfo.dwosversioninfosize = len(osinfo) osinfo.szcsdversion = space$(128) getversionexa osinfo g_nt4 = (osinfo.dwplatformid = ver_platform_win32_nt and _ osinfo.dwmajorversion <= 4)
' get max token size if g_nt4 then nt4querysecuritypackageinfo "ntlm", pspi else querysecuritypackageinfo "ntlm", pspi end if
if g_nt4 then nt4freecontextbuffer pspi else freecontextbuffer pspi end if
' allocate buffers for client and server messages pclientbuf = heapalloc(getprocessheap(), heap_zero_memory, _ cbmaxtoken) if pclientbuf = 0 then goto freeresourcesandexit end if
pserverbuf = heapalloc(getprocessheap(), heap_zero_memory, _ cbmaxtoken) if pserverbuf = 0 then goto freeresourcesandexit end if
' prepare client message (negotiate) . cbout = cbmaxtoken if not genclientcontext(asclient, ai, 0, 0, pclientbuf, cbout, _ fdone) then goto freeresourcesandexit end if
' prepare server message (challenge) . cbin = cbout cbout = cbmaxtoken if not genservercontext(asserver, pclientbuf, cbin, pserverbuf, _ cbout, fdone) then ' most likely failure: acceptservercontext fails with ' sec_e_logon_denied in the case of bad szuser or szpassword. ' unexpected result: logon will succeed if you pass in a bad ' szuser and the guest account is enabled in the specified domain. goto freeresourcesandexit end if
' prepare client message (authenticate) . cbin = cbout cbout = cbmaxtoken if not genclientcontext(asclient, ai, pserverbuf, cbin, pclientbuf, _ cbout, fdone) then goto freeresourcesandexit end if
' prepare server message (authentication) . cbin = cbout cbout = cbmaxtoken if not genservercontext(asserver, pclientbuf, cbin, pserverbuf, _ cbout, fdone) then goto freeresourcesandexit end if
sspvalidateuser = true
freeresourcesandexit:
' clean up resources if asclient.fhavectxthandle then if g_nt4 then nt4deletesecuritycontext asclient.hctxt else deletesecuritycontext asclient.hctxt end if end if
if asclient.fhavecredhandle then if g_nt4 then nt4freecredentialshandle asclient.hcred else freecredentialshandle asclient.hcred end if end if
if asserver.fhavectxthandle then if g_nt4 then nt4deletesecuritycontext asserver.hctxt else deletesecuritycontext asserver.hctxt end if end if
if asserver.fhavecredhandle then if g_nt4 then nt4freecredentialshandle asserver.hcred else freecredentialshandle asserver.hcred end if end if
if pclientbuf <> 0 then heapfree getprocessheap(), 0, pclientbuf end if
if pserverbuf <> 0 then heapfree getprocessheap(), 0, pserverbuf end if