Contents Index Managing user IDs and passwords C++ API user authentication example pdf/preface.pdf

UltraLite User's Guide
  Developing UltraLite Applications
    Adding user authentication to your application
      Managing user IDs and passwords

Embedded SQL user authentication example

The following code fragment performs user management and authentication for an embedded SQL UltraLite application.

A complete sample can be found in the Samples\UltraLite\esqlauth subdirectory of your SQL Anywhere directory. The code below is taken from Samples\UltraLite\esqlauth\sample.sqc.

app() {
   ...
/* Declare fields */
   EXEC SQL BEGIN DECLARE SECTION;
      char uid[31];
      char pwd[31]; 
   EXEC SQL END DECLARE SECTION;   
   ULEnableUserAuthentication( &sqlca );
   db_init( &sqlca );
   ...
   EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";
   if( SQLCODE == SQLE_NOERROR ) {
      printf("Enter new user ID and password\n" );
      scanf( "%s %s", uid, pwd );
      ULGrantConnectTo( &sqlca, 
         UL_TEXT( uid ), UL_TEXT( pwd ) );
      if( SQLCODE == SQLE_NOERROR ) {
         // new user added: remove DBA
         ULRevokeConnectFrom( &sqlca, UL_TEXT("DBA") );
      }
      EXEC SQL DISCONNECT;
   }
   // Prompt for password
    printf("Enter user ID and password\n" );
    scanf( "%s %s", uid, pwd );
    EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;

The code carries out the following tasks:

  1. Enable user authentication by calling ULEnableUserAuthentication.

  2. Initiate database functionality by calling db_init.

  3. Attempt to connect using the default user ID and password.

  4. If the connection attempt is successful, add a new user.

  5. If the new user is successfully added, delete the DBA user from the UltraLite database.

  6. Disconnect. An updated user ID and password is now added to the database.

  7. Connect using the updated user ID and password.

For more information, see ULGrantConnectTo function, and ULRevokeConnectFrom function.


Contents Index Managing user IDs and passwords C++ API user authentication example pdf/preface.pdf