.NET synchronization example

This example modifies an existing application to describe how to use .NET synchronization logic to handle the authenticate_user event. It creates a C# script for authenticate_user called AuthUser.cs. This script looks up the user's password in a table called user_pwd_table and authenticates the user based on that password.

To create your .NET synchronization script

  1. Add the table user_pwd_table to the database. Execute the following SQL statements in Interactive SQL:

    CREATE TABLE user_pwd_table (
      user_name  varchar(128) PRIMARY KEY NOT NULL,
      pwd        varchar(128)
    )
  2. Add a user and password to the table:

    INSERT INTO user_pwd_table VALUES( 'user1', 'myPwd' )
  3. Create a directory for your .NET assembly. For example, c:\mlexample.

  4. Create a file called AuthUser.cs with the following contents:

    See authenticate_user connection event.

    using System;
    using iAnywhere.MobiLink.Script; 
    namespace MLExample {
      
      public class AuthClass {
        private DBConnection  _conn;
    
        /// AuthClass constructor.
    
        public AuthClass( DBConnectionContext cc ) {
          _conn   = cc.GetConnection();
        }
    
        /// The DoAuthenticate method handles the 'authenticate_user'
        /// event.  
        /// Note: This method does not handle password changes for
        /// advanced authorization status codes.
     
        public void DoAuthenticate(
          ref int authStatus,
          string user,
          string pwd,
          string newPwd ) {
          DBCommand   pwd_command = _conn.CreateCommand();
          pwd_command.CommandText = "select pwd from user_pwd_table"
            + " where user_name = ? ";
          pwd_command.Prepare();
          // add a parameter for the user name
          DBParameter user_param = new DBParameter();
          user_param.DbType = SQLType.SQL_CHAR;
          // Set the size for SQL_VARCHAR.
          user_param.Size   = (uint)user.Length;
          user_param.Value  = user;
          pwd_command.Parameters.Add( user_param );
         // Fetch the password for this user.
          DBRowReader   rr  = pwd_command.ExecuteReader();
          object[]   pwd_row  = rr.NextRow();
          if( pwd_row == null ) {
            // User is unknown.
            authStatus  = 4000;
          } else {
            if( ((string)pwd_row[0]) == pwd ) {
              // Password matched.
              authStatus = 1000;
            } else {
              // Password did not match.
              authStatus = 4000;
            }
          }
          pwd_command.Close();
          rr.Close();
          return;
        }
      }
    }

    The MLExample.AuthClass.DoAuthenticate method handles the authenticate_user event. It accepts the user name and password and returns an authorization status code indicating the success or failure of the validation.

  5. Compile the file AuthUser.cs. You can do this on the command line or in Visual Studio .NET.

    For example, the following command line compiles AuthUser.cs and generate an Assembly named example.dll in c:\mlexample.

    csc /out:c:\mlexample\example.dll /target:library /reference:"%SQLANY11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" AuthUser.cs
  6. Register .NET code for the authenticate_user event. The method you need to execute (DoAuthenticate) is in the namespace MLExample and class AuthClass. Execute the following SQL:

    call ml_add_dnet_connection_script( 'ex_version', 'authenticate_user', 'MLExample.AuthClass.DoAuthenticate' )
    COMMIT
  7. Run the MobiLink server with the following option. This option causes MobiLink to load all assemblies in c:\myexample:

    -sl dnet ( -MLAutoLoadPath=c:\mlexample )

Now, when a user synchronizes with the version ex_version, they are authenticated with the password from the table user_pwd_table.