Automatically Login to a Domain
- From: "ringwood" <ringwood@xxxxxxxxxxxxx>
- Date: Wed, 31 May 2006 10:20:38 +0100
Hello,
we have a MIPSII device running CE 5.0 that we wish to automaitcally login
to a domain either when it starts up or immediately prior to some kind of
transaction that requires the device to be logged into the domain (e.g.
accessing network drives or print servers).
After some research I believe there are two functions available to me, those
being WNetAddConnection3 and the Credential Manager API (CredWrite).
Originally I tried using WNetAddConnection3 but I repeatedly keep getting
error code 53 (ERROR_BAD_NETPATH). Most examples I have found appear to
refer to network share folders or something like that, whereas I wish to log
on to a domain as I presume this will take care of all necessary logins. The
code is shown at the bottom of the post.
Whilst trying to solve the above problem I then stumbled across the
Credentials Manager API. This seems even better than the method above which
appeared to be confirmed when I did a comparison of the registry before and
after using the standard CE domain login dialog, as it showed an additional
entry with the key 'HKLM\Comm\Security\CredMan\Creds\1'. As such I went
ahead and implemented my own version of this code but yet again it did not
work even though the CredWrite call appeared to return the ERROR_SUCCESS
flag. The credential manager code is also shown at the bottom of this post:
My question is which is the best method and what am I doing wrong?
Any help is greatly appreciated,
Many Thanks
Roger
/// **************************** USING WNetAddConnection3
*********************************
NETRESOURCE tNetworkRes;
memset( &tNetworkRes, 0, sizeof( tNetworkRes) );
tNetworkRes.dwType = RESOURCETYPE_ANY;
tNetworkRes.lpLocalName = NULL;
// only microsft network provider supported
tNetworkRes.lpProvider = NULL;
// setup the remote name which will be my domain name e.g. MYDOMAIN
tNetworkRes.lpRemoteName = ptLanInfo->domain;
tNetworkRes.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN;
tNetworkRes.dwUsage = RESOURCEUSAGE_CONTAINER;
tNetworkRes.dwScope = RESOURCE_GLOBALNET;
// connect to the network
const DWORD dwERROR = WNetAddConnection3( AfxGetApp()->m_pMainWnd->m_hWnd,
&tNetworkRes,
ptLanInfo->password,
ptLanInfo->user,
0 );
// check if successful
if( ERROR_SUCCESS != dwERROR )
{
CString strError( L"" );
strError.Format( L"Unsuccessful connection to the network, error %u",
dwERROR );
LOG_DIAGNOSTIC_MESSAGE( MSGLISTSER_DIAGNOSTIC_ERROR, strError );
DebugBreak();
}
/// **************************** USING CREDENTIALS MANAGER
*********************************
CRED tCredentials;
tCredentials.dwVersion = CRED_VER_1;
// this is a domain password we want to store
tCredentials.dwType = CRED_TYPE_DOMAIN_PASSWORD;
// setup the username which is in the format MYDOMAIN\MyUserName
tCredentials.wszUser = ptLanInfo->user;
tCredentials.dwUserLen = wcslen( tCredentials.wszUser ) + 1;
// setup the target which will be my domain name e.g. MYDOMAIN
tCredentials.wszTarget = ptLanInfo->domain;
tCredentials.dwTargetLen = wcslen( tCredentials.wszTarget ) + 1;
// finally setup the password - I am assuming this is supposed to be in
WCHAR format
tCredentials.pBlob = reinterpret_cast< PBYTE >( ptLanInfo->password );
tCredentials.dwBlobSize = wcslen( ptLanInfo->password ) * sizeof( WCHAR );
tCredentials.dwFlags = 0;
// now write the credentials to the registry
const DWORD dwERROR = CredWrite( &tCredentials, 0 );
if( dwERROR != ERROR_SUCCESS )
{
DebugBreak();
}
// flush the registry to persist the credentials
CRegistry kRegKey;
kRegKey.Flush( );
kRegKey.Close( );
.
- Follow-Ups:
- Re: Automatically Login to a Domain
- From: John Spaith [MS]
- Re: Automatically Login to a Domain
- Prev by Date: Re: imgdecomp.dll help needed please!
- Next by Date: SmsOpen and SMS_E_RECEIVEHANDLEALREADYOPEN
- Previous by thread: Re: standby problems
- Next by thread: Re: Automatically Login to a Domain
- Index(es):
Relevant Pages
|