home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / Credentials / VC / Credentials.cpp next >
Encoding:
C/C++ Source or Header  |  1999-03-29  |  2.7 KB  |  122 lines

  1. /*
  2. **  Credential: Loggon to a current domain with alternate user name/password
  3. **
  4. **  Libraries:   activeds.lib, adsiid.lib
  5. **
  6. **  Copyright (c) 1998 Microsoft Corporation
  7. **  All Rights Reserved
  8. */
  9.  
  10. #include "stdafx.h"
  11.  
  12.  
  13. void ReportError(TCHAR *pTxt,DWORD err);
  14.  
  15. int main(int argc, char* argv[])
  16. {
  17.     IADs             *pNS = NULL,
  18.                      *pRoot=NULL,
  19.                      *pAuth=NULL;
  20.     
  21.     IADsOpenDSObject *pDSObj=NULL;
  22.  
  23.     VARIANT varDSRoot;
  24.  
  25.     TCHAR   adspath[MAX_PATH],username[255],password[255];
  26.  
  27.     HRESULT hr;
  28.  
  29.     hr = CoInitialize(NULL);
  30.     
  31.     //  Get the name of the root container for this domain.  
  32.     //  Read the Root DSE from the default DS,  which will be the DS for 
  33.     //  the local domain.  This will get us the name of the schema container,
  34.     //  which is stored in the "defaultNamingContext" operational attribute.
  35.  
  36.     hr = ADsGetObject(TEXT("LDAP://RootDSE"),
  37.                       IID_IADs,
  38.                       (void**)&pRoot);
  39.  
  40.        hr = pRoot->Get(TEXT("defaultNamingContext"),&varDSRoot);
  41.     _tprintf(TEXT("\nDomain Name is :%s\n"),varDSRoot.bstrVal);
  42.     pRoot->Release();
  43.  
  44.     _tcscpy(adspath,TEXT("LDAP://"));
  45.     _tcscat(adspath,varDSRoot.bstrVal);
  46.  
  47.     hr = ADsGetObject(TEXT("LDAP:"),
  48.                       IID_IADs,
  49.                       (void**)&pNS);
  50.  
  51.     hr = pNS->QueryInterface(IID_IADsOpenDSObject,(void**)&pDSObj);
  52.  
  53.     //
  54.     // Collect the username and password and bind to the Domain using these.
  55.     //
  56.  
  57.     if SUCCEEDED(hr) 
  58.     {
  59.         pNS->Release();
  60.         _tprintf(TEXT("\nusername:"));
  61.         _getts(username);
  62.         _tprintf(TEXT("\npassword:"));
  63.         _getts(password);
  64.         hr = pDSObj->OpenDSObject(adspath,username,password,ADS_SECURE_AUTHENTICATION,(IDispatch**)&pAuth);
  65.         // NOTE: Alternatively you can use an ADSI wrapper API: ADsOpenObject
  66.         if FAILED(hr) 
  67.         {
  68.             ReportError(TEXT("Bind Failed"),(DWORD)hr);
  69.         }
  70.         else
  71.         {
  72.             _tprintf(TEXT("Successfully logon!"));
  73.  
  74.         }
  75.     }
  76.  
  77.  
  78.     if (pAuth)
  79.     {
  80.         pAuth->Release();
  81.     }
  82.     if (pDSObj)
  83.     {
  84.         pDSObj->Release();
  85.     }
  86.  
  87.     CoUninitialize();
  88.     return 0;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. // Simple error message reporter
  95.  
  96. void ReportError(TCHAR *pTxt,DWORD err)
  97. {
  98.     DWORD   dwStatus;
  99.     TCHAR   *pBuf;
  100.  
  101.     dwStatus = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|
  102.                              FORMAT_MESSAGE_ALLOCATE_BUFFER|
  103.                              FORMAT_MESSAGE_IGNORE_INSERTS,
  104.                              NULL,
  105.                              err,
  106.                              LANG_NEUTRAL,
  107.                              (USHORT*)&pBuf,
  108.                              64,
  109.                              NULL); 
  110.     if (dwStatus != 0) 
  111.     {
  112.         _tprintf(TEXT("%s %s"),pTxt,pBuf);
  113.         LocalFree(pBuf);
  114.     } else 
  115.     {
  116.         _tprintf(TEXT("%s %X\n"),pTxt,err);
  117.     }
  118. }
  119.  
  120.  
  121.  
  122.