このサンプルは、.NET 同期論理を使用して authenticate_user イベントを処理する方法を表示するように既存のアプリケーションを変更します。このサンプルは、AuthUser.cs という名前の authenticate_user 用の C# スクリプトを作成します。このスクリプトは、user_pwd_table というテーブル内でユーザのパスワードを検索し、そのパスワードに基づいてユーザを認証します。
テーブル user_pwd_table をデータベースに追加します。Interactive SQL で次の 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 アセンブリ用のディレクトリを作成します例: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; // 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 で実行できます。
たとえば、次のコマンドラインは AuthUser.cs をコンパイルし、example.dll という名前のアセンブリを c:\mlexample に生成します。
csc /out:c:\mlexample\example.dll /target:library /reference:"%SQLANY16%\Assembly\V2\iAnywhere.MobiLink.Script.dll" AuthUser.cs |
authenticate_user イベント用の .NET コードを登録します。実行する必要があるメソッドは、ネームスペース MLExample とクラス AuthClass にあります。次の SQL を実行します。
CALL ml_add_dnet_connection_script('ex_version', 'authenticate_user', 'MLExample.AuthClass.DoAuthenticate'); COMMIT |
次のオプションで Mobile Link サーバを実行します。このオプションによって、Mobile Link が c:\myexample 内のすべてのアセンブリをロードします。
-sl dnet (-MLAutoLoadPath=c:\mlexample) |
これで、ユーザがバージョン ex_version と同期するときに、テーブル user_pwd_table のパスワードで認証されるようになります。
![]() |
DocCommentXchange で意見交換できます
|
Copyright © 2013, SAP AG or an SAP affiliate company. - SAP Sybase SQL Anywhere 16.0 |