Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » MobiLink - 服务器管理 » MobiLink 服务器 API » 使用 .NET 编写同步脚本

 

.NET 同步示例

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

 ♦  创建 .NET 同步脚本
  1. 将 user_pwd_table 表添加到数据库中。在 Interactive SQL 中执行以下 SQL 语句:

    CREATE TABLE user_pwd_table (
      user_name  varchar(128) PRIMARY KEY NOT NULL,
      pwd        varchar(128)
    )
  2. 向该表中添加一个用户及其口令:

    INSERT INTO user_pwd_table VALUES('user1', 'myPwd')
  3. 为您的 .NET 程序集创建一个目录。例如,c:\mlexample

  4. 创建一个名为 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;
    
            // 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 事件。它会接受用户名和口令,并返回指示校验成功或失败的授权状态码。

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

    例如,以下命令行会编译文件 AuthUser.cs,并在 c:\mlexample 中生成名为 example.dll 的程序集。

    csc /out:c:\mlexample\example.dll /target:library /reference:"%SQLANY12%\Assembly\V2\iAnywhere.MobiLink.Script.dll" AuthUser.cs
  6. 为 authenticate_user 事件注册 .NET 代码。需要执行的方法 (DoAuthenticate) 位于 MLExample 命名空间和 AuthClass 类中。执行以下 SQL 命令:

    CALL ml_add_dnet_connection_script('ex_version', 'authenticate_user', 'MLExample.AuthClass.DoAuthenticate');
    COMMIT
  7. 使用以下选项运行 MobiLink 服务器。使用此选项时,MobiLink 将装载 c:\myexample 中的所有程序集:

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

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