Contents Index User-defined start classes MobiLink .NET API Reference

MobiLink Synchronization User's Guide
  Writing Synchronization Scripts in .NET

.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.

First, add the table user_pwd_table to the database. Execute the following in Interactive SQL:

CREATE TABLE user_pwd_table (
  user_name  varchar(128) PRIMARY KEY NOT NULL,
  pwd        varchar(128)
)

Next, add a user and password to the table:

INSERT INTO user_pwd_table VALUES( 'user1', 'myPwd' )

Create a directory for your .NET assembly. For example:

mkdir c:\mlexample

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

using System;
using iAnywhere.MobiLink.Script;
namespace MLExample
{
  /// <summary>
  /// A simple example class the authenticates a user.
  /// </summary>
  /// <remarks>
  /// This simple example class will compare the password
  /// given for a user with the password in a table and accept
  /// or reject the authentication. We don't handle changing
  /// user password. To handle changing the password we could
  /// just update the user password table.</remarks>
  public class AuthClass
  {
     private DBConnection  _conn;
  /// <summary>
  /// Create the instance of AuthClass for the given MobiLink
  /// connection.</summary>
  /// <remarks>
  /// This instance will live for the duration of
  /// the MobiLink connection. This means that this instance
  /// will authenticate many users just as a connection will
  /// handle many synchronizations.</remarks>
  /// <param name="cc">The connection that owns this
  ///  instance.</param>
  public AuthClass( DBConnectionContext cc )
  {
     _conn   = cc.GetConnection();
  }
  /// <summary>
  /// Handler for 'authenticate_user' MobiLink event.
  /// </summary>
  /// <remarks>
  /// Handle the 'authenticate_user' event in the simplest way
  /// possible. Don't handle password changes for any advanced
  /// authStatus Codes.</remarks>
  /// <param name="authStatus">The status for this
  /// authenticate attempt.</param>
  /// <param name="user">Name of the user to authenticate.
  /// </param>
  /// <param name="pwd">Password the user is authenticating
  /// with.</param>
  /// <param name="newPwd">The new password for the
  /// authenticating user.</param>
  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 param for the user name that we can set later.
      DBParameter user_param = new DBParameter();
      user_param.DbType = SQLType.SQL_CHAR;
      // we need to 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;
  }
  }
}

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

For example, the following command line will compile AuthUser.cs and generate an Assembly named example.dll in c:\mlexample. Substitute your install directory for asany9.

csc /out:c:\mlexample\example.dll /target:library /reference:\asany9\win32\iAnywhere.MobiLink.Script.dll AuthUser.cs

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

Next, run the MobiLink synchronization 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.


Contents Index User-defined start classes MobiLink .NET API Reference