Tutorial ed esempi/Login e OTP

Login e OTP

In questo esempio viene implementata una chiamata API che integra la verifica della login con la verifica OTP in un unico metodo utile ad esempio ad essere invocato direttamente da un app mobile.


/// Verifica login, risponde con una GenericResponse
/// Se Otp enable → restituisce Message = "OTP"
/// Se Otp disable → restituisce Message = "" e ReturnGuid = Guid Utente
/// Se viene passato anche il parametro OtpCode viene effetuatua la verifica Otp
[HttpPost]
public async Task LoginVerify(string email, string password, long OtpCode)
{
    UsersClient _userClient = new UsersClient(UsersClient.EndpointConfiguration.BasicHttpBinding_IUsers, SdmEndpoints.Value.ServiceUsers);
    PlatformClient _platformClient = new PlatformClient(PlatformClient.EndpointConfiguration.BasicHttpBinding_IPlatform, SdmEndpoints.Value.ServicePlatform);
    ServiceClicency.GenericResponse respResult = new ServiceClicency.GenericResponse();
    try
    {
        Guid userGuid = await _userClient.LoginVerifyAsync(email, password);
        if (userGuid.CompareTo(Guid.Empty) == 0)
        {
            // login non valida
            respResult = new ServiceClicency.GenericResponse()
            {
                Result = false,
                Message = MessageLangKey.UnAthorized,
                ReturnGuid = Guid.Empty,
                MessageSource = ServiceClicency.ResponseMessageSourceEnum.Custom
            };
        }
        else
        {
            //verifica se richiede OTP
            string EnableEmailOtpLoginTmp = await _platformClient.GetSettingsValueByKeyAsync("EnableOtpLoginVerify");
            Boolean.TryParse(EnableEmailOtpLoginTmp, out bool EnableEmailOtpLogin);
            if (EnableEmailOtpLogin)
            {
                //OTP abilitato
                
                if (OtpCode > 0 && userGuid.CompareTo(Guid.Empty) != 0)
                {
                    //OTP valido
                    ServiceUsers.GenericResponse resp = await _userClient.VerifyOtpAsync(userGuid, OtpCode);
                    if (resp.Result == false)
                    {
                        // verifica fallita
                        Tools.Mapper.Copy(resp, respResult);
                    }
                    else
                    {
                        // verifica ok
                        respResult = new ServiceClicency.GenericResponse()
                        {
                            Result = true,
                            ReturnGuid = userGuid
                        };
                    }
                }
                else
                {
                    //OTP non valido
                    await _userClient.OtpCodeRequestAsync(userGuid);
                    respResult = new ServiceClicency.GenericResponse()
                    {
                        Result = userGuid.CompareTo(Guid.Empty) == 0 ? false : true,
                        Message = "OTP",
                        ReturnGuid = Guid.Empty,
                        MessageSource = ServiceClicency.ResponseMessageSourceEnum.Custom
                    };
                }
            }
            else
            {
                //OTP non abilitato
                respResult = new ServiceClicency.GenericResponse()
                {
                    Result = userGuid.CompareTo(Guid.Empty) == 0 ? false : true,
                    ReturnGuid = userGuid
                };
            }
        }
        return respResult;
    }
    catch (Exception e)
    {
        return new ServiceClicency.GenericResponse()
        {
            Result = false,
            Message = e.Message,
            MessageSource = ServiceClicency.ResponseMessageSourceEnum.Exception
        };
    }
}

An error has occurred. This application may no longer respond until reloaded. Reload 🗙