Collection Contents 上一页 下一页 PDF

MobiLink 管理指南

使用 .NET 编写同步脚本

.NET 同步示例


此示例通过修改一个现有应用程序,讲解如何使用 .NET 同步逻辑来处理 authenticate_user 事件。它为名为 AuthUser.cs 的 authenticate_user 事件创建一个 C# 脚本。该脚本在名为 user_pwd_table 的表中查找用户的口令,并根据该口令鉴定用户。

注意: 
本节通过一个简单的示例来说明基本的 .NET 同步逻辑。通常,使用自定义用户鉴定机制的原因有:与现有 DBMS 用户鉴定方案的集成,或提供自定义功能,例如,最小口令长度或口令到期时间等。MobiLink 还提供内置缺省机制。

有关 MobiLink 鉴定的详细信息,请参见选择用户鉴定机制

首先,在数据库中添加 user_pwd_table 表。在 Interactive SQL 中执行以下语句:

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

然后,在该表中添加一个用户及其口令:

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

为您的 .NET 程序集创建一个目录。例如:

mkdir c:\mlexample

创建一个名为 AuthUser.cs 的文件,并使该文件具有以下内容:

有关详细信息,请参见 authenticate_user 连接事件

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;
  }
  }
}

MLExample.AuthClass.DoAuthenticate 方法可处理 authenticate_user 事件。它会接收用户名和口令,并返回指示校验成功或失败的授权状态码。

编译 AuthUser.cs 文件。可以使用命令行或 Visual Studio .NET 进行编译。

例如,以下命令行将编译 AuthUser.cs 并在 c:\mlexample 中生成一个名为 example.dll 的程序集。请用您的安装目录替换 asany9

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

为 authenticate_user 事件注册 .NET 代码。需要执行的方法 (DoAuthenticate) 位于 MLExample 命名空间和 AuthClass 类中。执行下列 SQL 命令:

call ml_add_dnet_connection_script( 'ex_version', 'authenticate_user', 'MLExample.AuthClass.DoAuthenticate' )
COMMIT

然后,使用以下选项运行 MobiLink 同步服务器。使用此选项时,MobiLink 将装载 c:\myexample 中的所有程序集:

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

现在,当用户与版本 ex_version 同步时,将使用 user_pwd_table 表中的口令对其进行鉴定。


Collection Contents 上一页 下一页 PDF