User authentication

A complete sample can be found in the %SQLANYSAMP12%\UltraLite\esqlauth directory. The code below is taken from %SQLANYSAMP12%\UltraLite\esqlauth\sample.sqc.



//embedded SQL
app() {
   ...
/* Declare fields */
   EXEC SQL BEGIN DECLARE SECTION;
      char uid[31];
      char pwd[31];
   EXEC SQL END DECLARE SECTION;
   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. Initiate database functionality by calling db_init.

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

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

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

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

  6. Connect using the updated user ID and password.

 See also