Writing Synchronization Scripts in .NET
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.
Note:This section provides a simple example to illustrate basic .NET synchronization logic. Typically, reasons for using a custom user authentication mechanism include integration with existing DBMS user authentication schemes, or supplying custom features, such as minimum password length or password expiry. MobiLink also has a built-in default mechanism.For more information about MobiLink authentication, see Choosing a user authentication mechanism. |
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:
For more information, 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;
// 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;
}
}
}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.
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.
SQL Anywhere Studio 9.0.2
Copyright © 1989–2004 Sybase, Inc. Portions copyright © 2001–2004 iAnywhere Solutions, Inc. All rights reserved.