Lesson 1: Create a Java or .NET class for custom authentication (server-side)

In this lesson, you compile a class containing Java or .NET logic for custom authentication.

MobiLink server API for .NET

To execute .NET synchronization logic, the MobiLink server must have access to the classes in iAnywhere.MobiLink.Script.dll. iAnywhere.MobiLink.Script.dll contains a repository of MobiLink .NET server API classes to utilize in your .NET methods. When you compile your .NET class, you reference iAnywhere.MobiLink.Script.dll.

MobiLink server API for Java

To execute Java synchronization logic, the MobiLink server must have access to the classes in mlscript.jar. mlscript.jar contains a repository of MobiLink Java server API classes to utilize in your Java methods. When you compile your Java class, you reference mlscript.jar.

To create your Java or .NET class for custom authentication

  1. Create a class called MobiLinkAuth using Java or .NET.

    The MobiLinkAuth class includes the authenticateUser method used for the authenticate_user synchronization event. The authenticate_user event provides parameters for the user and password. You return the authentication result in the authentication_status inout parameter.

    For Java, type the following:

    import ianywhere.ml.script.*;
    
    public class MobiLinkAuth
    {
    
     public void authenticateUser (
      ianywhere.ml.script.InOutInteger authentication_status,
      String user,
      String pwd,
      String newPwd )
     {
       // to do...
    
     }
    
    }

    For .NET, type the following:

    using iAnywhere.MobiLink.Script;
    
    public class MobiLinkAuth
    {
      public void authenticateUser  (
        ref int authentication_status,
        string user,
        string pwd,
        string newPwd)
      { 
        
       // to do...
    
      }
    }
  2. Write the authenticateUser method.

    This tutorial illustrates a very simple case of custom user authentication. Authentication succeeds if the user name starts with "ML".

    Note

    You register the authenticateUser method for the authenticate_user synchronization event in Lesson 2: Register your Java or .NET scripts for the authenticate_user event.

    For Java, type the following:

    public void authenticateUser (
     ianywhere.ml.script.InOutInteger authentication_status,
     String user,
     String pwd,
     String newPwd ) {
    
    if(user.substring(0,1)=="ML") {
            // success: an auth status code of 1000
            authentication_status.setValue(1000);
        } else {
            // fail: an authentication_status code of 4000
            authentication_status.setValue(4000);
        }
    }

    For .NET, type the following:

    public void authenticateUser(
     ref int authentication_status,
     string user,
     string pwd,
     string newPwd ) {
    
        if(user.Substring(0,2)=="ML") {
            // success: an auth status code of 1000
            authentication_status = 1000;
        } else { 
            // fail: and authentication_status code of 4000
            authentication_status = 4000;
        }
    }
  3. Compile the MobiLinkAuth class.

    • For Java, save the file as MobiLinkAuth.java in c:\mlauth. Navigate to c:\mlauth. To compile the file, run the following command. (Replace install-dir with the location of your SQL Anywhere installation.)
      javac MobiLinkAuth.java -classpath "install-dir\java\mlscript.jar"

      The MobiLinkAuth.class file is generated.

    • For .NET, save the file as MobiLinkAuth.cs in c:\mlauth.
    • At a command prompt, navigate to c:\mlauth. To compile the file, run the following command. (Replace install-dir with the location of your SQL Anywhere installation.)
      csc /out:c:\mlauth\MobiLinkAuth.dll /target:library /reference:"%SQLANY11%\Assembly\v2\iAnywhere.MobiLink.Script.dll" MobiLinkAuth.cs

      The MobiLinkAuth.dll assembly is generated.

Further reading

For more information about the authenticate_user event, including a table of authentication_status return codes, see authenticate_user connection event.

For more information about implementing custom authentication using Java or .NET, see Java and .NET user authentication.

For a detailed example of Java custom authentication, see Java synchronization example.

For a detailed example of .NET custom authentication, see .NET synchronization example.