コード例 #1
0
        /// <summary>
        /// Envía una solicitud de autenticación al servicio Aspen.
        /// </summary>
        /// <param name="customPayload">Información adicional para agregar en el Payload de la solicitud.</param>
        /// <param name="useCache">Cuando es <see langword="true" /> se utiliza el último token de autenticación generado en la sesión.</param>
        /// <returns>Instancia de <see cref="ISession" /> que se puede utilizar para solicitar más recursos de información al servicio Aspen.</returns>
        public ISession Authenticate(IDictionary <string, object> customPayload = null, bool useCache = true)
        {
            IRestRequest request = new AspenRequest(this, Routes.Auth.Signin, Method.POST, false);

            this.AddRequiredHeaders(request, customPayload);

            if (useCache)
            {
                this.AuthToken = CacheStore.GetCurrentToken(this.AppScope);
                if (this.AuthToken != null)
                {
                    return(this);
                }
            }

            string DecodeJsonResponse(string jwt) => this.decoder.Decode(jwt, this.identityProvider.ApiSecret, true);

            IAuthToken authToken;

            switch (this.AppScope)
            {
            case AppScope.Autonomous:
                authToken = this.Execute <AuthToken>(request, DecodeJsonResponse);
                break;

            case AppScope.Delegated:
                authToken = this.Execute <UserAuthToken>(request, DecodeJsonResponse);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            this.AuthToken = authToken;
            CacheStore.SetCurrentToken(authToken, this.AppScope);
            return(this);
        }