Ejemplo n.º 1
0
        /// <summary>
        /// Ensures the security token for non-AD scenarios is valid and renews if it near expiration or expired
        /// </summary>
        public static void EnsureValidToken <TService>(this ServiceProxy <TService> proxy)
            where TService : class
        {
            if (proxy.ServiceConfiguration.AuthenticationType != AuthenticationProviderType.ActiveDirectory &&
                proxy.SecurityTokenResponse != null)
            {
                DateTime validTo = proxy.SecurityTokenResponse.Token.ValidTo;

                if (DateTime.UtcNow.AddMinutes(15) >= validTo)
                {
                    //XrmCoreEventSource.Log.SecurityTokenRefreshRequired(validTo.ToString("u"), true);

                    try
                    {
                        proxy.Authenticate();
                    }
                    catch (Exception ex)
                    {
                        StringBuilder messageBuilder = ex.ToErrorMessageString();

                        //XrmCoreEventSource.Log.SecurityTokenRefreshFailure(validTo.ToString("u"), messageBuilder.ToString());

                        // Rethrow if current token is lost during authentication attempt
                        // or if current token has expired
                        if (proxy.SecurityTokenResponse == null ||
                            DateTime.UtcNow >= validTo)
                        {
                            throw;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task AuthenticateAsync(this ServiceProxy <IOrganizationService> sdk, Entity entity)
        {
            var t = Task.Factory.StartNew(() =>
            {
                sdk.Authenticate();
            });

            await t;
        }