Contents Index Embedded SQL user authentication example Java user authentication example pdf/preface.pdf

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

C++ API user authentication example

The following code fragment performs user management and authentication for a C++ API UltraLite application.

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

ULEnableUserAuthentication( &sqlca );
db.Open() ;
   if( conn.Open( &db, 
                  UL_TEXT( "dba" ), 
                  UL_TEXT( "sql" ) ) ){
      // prompt for new user ID and password
      printf("Enter new user ID and password\n" );
      scanf( "%s %s", uid, pwd );
      if( conn.GrantConnectTo( uid, pwd ) ){
         // new user added, remove dba
         conn.RevokeConnectFrom( UL_TEXT("dba") );
      }
      conn.Close();
   } 
   // regular connection
   printf("Enter user ID and password\n" );
   scanf( "%s %s", uid, pwd );
   if( conn.Open( &db, uid, pwd )){
   ...

The code carries out the following tasks:

  1. Initiate database functionality by opening the database object.

  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.

For more information, see GrantConnectTo method, and RevokeConnectFrom method.


Contents Index Embedded SQL user authentication example Java user authentication example pdf/preface.pdf