API > API > Login ed uso del token sostitutivo

Login ed uso del token sostitutivo

Come effettuare il login

Ad ogni utente è associato un token univoco, con validità di 30 minuti, che attesta l'avvenuta autenticazione al servizio.
Se si possiede un token valido è possibile effettuare le chiamate ai metodi del WSCommon e WsEndUser. A seguito di ogni chiamata di un qualsiasi metodo di Aruba WSCommon o WsEndUser infatti il servizio verifica l'esistenza un token valido per l'account specificato. Se questo non esiste, o se il token dovesse risultare scaduto, sarà necessario autenticarsi nuovamente per ottenerlo.
In realtà il token è un sostituto della password che ha una validità temporanea.
Le API autenticano con la coppia username + password oppure con username+token valido.
Non è obbligatorio ottenere il token per richiamare le API.
I metodi GetUserAuthenticationToken (WsCommon) e GetUserAuthenticationToken (WsEndUser) non sono richiamabili con username + token per motivi di sicurezza.

L'esempio genera un token di autenticazione e lo associa all'account indicato.
 
//IWsCommon.GetUserAuthenticationToken Method (c# .NET) 
private static void LoginToWsCommon(WsCommonClient client)
{
    //specifica le credenziali dell'account 
    client.ClientCredentials.UserName.UserName = "ARU-0000";
    client.ClientCredentials.UserName.Password = "0123456789";

    try
    {
        WsResultOfUserToken result = client.GetUserAuthenticationToken();

        if (result.Success)
        {
            //il metodo è andato a buon fine
        }
        else
        {
            throw new Exception(result.ResultMessage);
        }
    }
    catch (MessageSecurityException msEx)
    {
        //utente non autenticato 
        throw new Exception(msEx.Message);
    }
    catch (Exception ex)
    {
        //eccezione generica 
        throw new Exception(ex.Message);
    }
}

//IWsEndUser.GetUserAuthenticationToken Method (c# .NET) 
private static void LoginToWsEndUser(WsEndUserClient client)
{
    //specifica le credenziali dell'account 
    client.ClientCredentials.UserName.UserName = "ARU-0000";
    client.ClientCredentials.UserName.Password = "0123456789";

    try
    {
        var result = client.GetUserAuthenticationToken();

        if (result.Success)
        {
            //il metodo è andato a buon fine
        }
        else
        {
            throw new Exception(result.ResultMessage);
        }
    }
    catch (MessageSecurityException msEx)
    {
        //utente non autenticato 
        throw new Exception(msEx.Message);
    }
    catch (Exception ex)
    {
        //eccezione generica 
        throw new Exception(ex.Message);
    }
}
//IWsCommon.loginToWsCommon Method (JAVA)
private static void loginToWsCommon(IWsCommon client) throws Exception
{    	
    //specifica le credenziali dell'account 
    ((BindingProvider)client).getRequestContext()
            .put(BindingProvider.USERNAME_PROPERTY, "ARU-0000");	
    ((BindingProvider)client).getRequestContext()
            .put(BindingProvider.PASSWORD_PROPERTY, "0123456789");	

    try
    {
        WsResultOfUserToken result = client.getUserAuthenticationToken();

        if (result.isSuccess())
        {
            //il metodo è andato a buon fine
        }
        else
        {
            throw new Exception(result.getResultMessage());
        }
    }
    catch (SecurityException  ex)
    {
        //utente non autenticato
        throw new Exception(ex.getMessage());
    }
    catch (Exception  ex)
    {
        //eccezione generica 
        System.out.println(ex);
    }
}

//IWsEndUser.loginToWsEndUser Method (JAVA)
private static void loginToWsEndUser(IWsEndUser client) throws Exception
{    	
    //specifica le credenziali dell'account 
    ((BindingProvider)client).getRequestContext()
            .put(BindingProvider.USERNAME_PROPERTY, "ARU-0000");	
    ((BindingProvider)client).getRequestContext()
            .put(BindingProvider.PASSWORD_PROPERTY, "0123456789");	

    try
    {
        WsResultOfUserToken result = client.getUserAuthenticationToken();

        if (result.isSuccess())
        {
            //il metodo è andato a buon fine
        }
        else
        {
            throw new Exception(result.getResultMessage());
        }
    }
    catch (SecurityException  ex)
    {
        //utente non autenticato
        throw new Exception(ex.getMessage());
    }
    catch (Exception  ex)
    {
        //eccezione generica 
        System.out.println(ex);
    }
}