Exemplo n.º 1
0
        private void Renew(AdalAccessToken token)
        {
            TracingAdapter.Information(Resources.UPNRenewTokenTrace, token.AuthResult.AccessTokenType, token.AuthResult.ExpiresOn,
                                       token.AuthResult.IsMultipleResourceRefreshToken, token.AuthResult.TenantId, token.UserId);
            var user = token.AuthResult.UserInfo;

            if (user != null)
            {
                TracingAdapter.Information(Resources.UPNRenewTokenUserInfoTrace, user.DisplayableId, user.FamilyName,
                                           user.GivenName, user.IdentityProvider, user.UniqueId);
            }
            if (IsExpired(token))
            {
                TracingAdapter.Information(Resources.UPNExpiredTokenTrace);
                AuthenticationResult result = AcquireToken(token.Configuration, ShowDialog.Never, token.UserId, null);

                if (result == null)
                {
                    throw new Exception(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }
Exemplo n.º 2
0
        private void Renew(AdalAccessToken token)
        {
            TracingAdapter.Information(Resources.UPNRenewTokenTrace, token.AuthResult.AccessTokenType, token.AuthResult.ExpiresOn,
                token.AuthResult.IsMultipleResourceRefreshToken, token.AuthResult.TenantId, token.UserId);
            var user = token.AuthResult.UserInfo;
            if (user != null)
            {
                TracingAdapter.Information(Resources.UPNRenewTokenUserInfoTrace, user.DisplayableId, user.FamilyName,
                    user.GivenName, user.IdentityProvider, user.UniqueId);
            }
            if (IsExpired(token))
            {
                TracingAdapter.Information(Resources.UPNExpiredTokenTrace);
                AuthenticationResult result = AcquireToken(token.Configuration, ShowDialog.Never, token.UserId, null);

                if (result == null)
                {
                    throw new AuthenticationException(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }
Exemplo n.º 3
0
        private bool IsExpired(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_ACCESS_TOKEN") != null)
            {
                return true;
            }
#endif

            return token.AuthResult.ExpiresOn - DateTimeOffset.Now < thresholdExpiration;
        }
Exemplo n.º 4
0
        private bool IsExpired(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_ACCESS_TOKEN") != null)
            {
                return(true);
            }
#endif

            return(token.AuthResult.ExpiresOn - DateTimeOffset.Now < thresholdExpiration);
        }
Exemplo n.º 5
0
        private string GetRefreshToken(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_REFRESH_TOKEN") != null)
            {
                // We can't force an expired refresh token, so provide a garbage one instead
                const string fakeToken = "This is not a valid refresh token";
                return(Convert.ToBase64String(Encoding.ASCII.GetBytes(fakeToken)));
            }
#endif
            return(token.AuthResult.RefreshToken);
        }
        private bool IsExpired(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_ACCESS_TOKEN") != null)
            {
                return(true);
            }
#endif
            var expiration          = token.AuthResult.ExpiresOn;
            var currentTime         = DateTimeOffset.UtcNow;
            var timeUntilExpiration = expiration - currentTime;
            TracingAdapter.Information(Resources.UPNTokenExpirationCheckTrace, expiration, currentTime, expirationThreshold,
                                       timeUntilExpiration);
            return(timeUntilExpiration < expirationThreshold);
        }
Exemplo n.º 7
0
        private bool IsExpired(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_ACCESS_TOKEN") != null)
            {
                return true;
            }
#endif
            var expiration = token.AuthResult.ExpiresOn;
            var currentTime = DateTimeOffset.UtcNow;
            var timeUntilExpiration = expiration - currentTime;
            TracingAdapter.Information(Resources.UPNTokenExpirationCheckTrace, expiration, currentTime, expirationThreshold,
                timeUntilExpiration);
            return timeUntilExpiration < expirationThreshold;
        }
        private void Renew(AdalAccessToken token)
        {
            if (IsExpired(token))
            {
                AuthenticationResult result = AcquireToken(token.Configuration, ShowDialog.Never, token.UserId, null);

                if (result == null)
                {
                    throw new Exception(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }
        private void Renew(AdalAccessToken token)
        {
            if (IsExpired(token))
            {
                AuthenticationResult result = AcquireToken(token.Configuration, ShowDialog.Never, token.UserId, null);

                if (result == null)
                {
                    throw new Exception(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }
Exemplo n.º 10
0
        private void Renew(AdalAccessToken token)
        {
            AuthenticationResult result = null;
            Exception            ex     = null;

            if (IsExpired(token))
            {
                var thread = new Thread(() =>
                {
                    var context = CreateContext(token.Configuration);
                    try
                    {
                        result = context.AcquireTokenByRefreshToken(GetRefreshToken(token),
                                                                    token.Configuration.ClientId,
                                                                    token.Configuration.ResourceClientUri);
                    }
                    catch (Exception threadEx)
                    {
                        ex = threadEx;
                    }
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Name = "AcquireTokenThread";
                thread.Start();
                thread.Join();

                if (ex != null)
                {
                    throw new AadAuthenticationCantRenewException(Resources.ExpiredRefreshToken, ex);
                }

                if (result == null)
                {
                    throw new Exception(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }
Exemplo n.º 11
0
 private void Renew(AdalAccessToken token)
 {
     if (IsExpired(token))
     {
         var context = CreateContext(token.Configuration);
         try
         {
             var authResult = context.AcquireTokenByRefreshToken(GetRefreshToken(token),
                                                                 token.Configuration.ClientId,
                                                                 token.Configuration.ResourceClientUri);
             if (authResult == null)
             {
                 throw new Exception(Resources.ExpiredRefreshToken);
             }
             token.AuthResult = authResult;
         }
         catch (Exception ex)
         {
             throw new AadAuthenticationCantRenewException(Resources.ExpiredRefreshToken, ex);
         }
     }
 }
Exemplo n.º 12
0
        private string GetRefreshToken(AdalAccessToken token)
        {
#if DEBUG
            if (Environment.GetEnvironmentVariable("FORCE_EXPIRED_REFRESH_TOKEN") != null)
            {
                // We can't force an expired refresh token, so provide a garbage one instead
                const string fakeToken = "This is not a valid refresh token";
                return Convert.ToBase64String(Encoding.ASCII.GetBytes(fakeToken));
            }
#endif
            return token.AuthResult.RefreshToken;
        }
Exemplo n.º 13
0
 private void Renew(AdalAccessToken token)
 {
     if (IsExpired(token))
     {
         var context = CreateContext(token.Configuration);
         try
         {
             var authResult = context.AcquireTokenByRefreshToken(GetRefreshToken(token),
                 token.Configuration.ClientId,
                 token.Configuration.ResourceClientUri);
             if (authResult == null)
             {
                 throw new Exception(Resources.ExpiredRefreshToken);
             }
             token.AuthResult = authResult;
         }
         catch (Exception ex)
         {
             throw new AadAuthenticationCantRenewException(Resources.ExpiredRefreshToken, ex);
         }
     }
 }
Exemplo n.º 14
0
        private void Renew(AdalAccessToken token)
        {
            AuthenticationResult result = null;
            Exception ex = null;

            if (IsExpired(token))
            {
                var thread = new Thread(() =>
                {
                    var context = CreateContext(token.Configuration);
                    try
                    {
                        result = context.AcquireTokenByRefreshToken(GetRefreshToken(token),
                            token.Configuration.ClientId,
                            token.Configuration.ResourceClientUri);
                    }
                    catch (Exception threadEx)
                    {
                        ex = threadEx;
                    }
                });
                thread.SetApartmentState(ApartmentState.STA);
                thread.Name = "AcquireTokenThread";
                thread.Start();
                thread.Join();

                if (ex != null)
                {
                    throw new AadAuthenticationCantRenewException(Resources.ExpiredRefreshToken, ex);
                }

                if (result == null)
                {
                    throw new Exception(Resources.ExpiredRefreshToken);
                }
                else
                {
                    token.AuthResult = result;
                }
            }
        }