UltraLite Static C++ User's Guide
Adding Non Data Access Features to UltraLite Applications
Adding user authentication to your application
Managing user IDs and passwords
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:
Initiate database functionality by opening the database object.
Attempt to connect using the default user ID and password.
If the connection attempt is successful, add a new user.
If the new user is successfully added, delete the DBA user from the UltraLite database.
Disconnect. An updated user ID and password is now added to the database.
Connect using the updated user ID and password.
For more information, see GrantConnectTo method, and RevokeConnectFrom method.