public static void RemoveToken(CacheToken token) { if (_tokens.Contains(token)) { _tokens.Remove(token); } }
public static void AddToken(CacheToken token) { CacheToken _removeToken = null; foreach (var t in _tokens) { if (t.Name == token.Name && t.ResourceId == token.ResourceId && t.TenantId == token.TenantId) { _removeToken = t; } } if (_removeToken != null) { _tokens.Remove(_removeToken); } _tokens.Add(token); }
public async Task <CacheToken> AuthenticateEndpoint(Endpoint endpoint, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { if (endpoint.IsActive) { AuthenticationResult results = null; IPlatformParameters platformParameters = _azurePlatformParameters.GetPlatformParameters(endpoint.UseBroker); try { var authContext = new AuthenticationContext(endpoint.Authority); //fixes for security groups in iOS per //https://aka.ms/adal-net-ios-keychain-access #if iOS authContext.iOSKeychainSecurityGroup = endpoint.iOSKeychainSecurityGroup; #endif _logBuilder.Clear(); if (endpoint.EnableLogging) { LoggerCallbackHandler.LogCallback = AdalLog; LoggerCallbackHandler.PiiLoggingEnabled = endpoint.EnableLogging; } if (string.IsNullOrEmpty(endpoint.ExtraParameters)) { results = await authContext.AcquireTokenAsync(endpoint.ResourceId, endpoint.ApplicationId, new Uri(endpoint.RedirectUri), platformParameters).WithTimeout(5000); } else { results = await authContext.AcquireTokenAsync(endpoint.ResourceId, endpoint.ApplicationId, new Uri(endpoint.RedirectUri), platformParameters, UserIdentifier.AnyUser, endpoint.ExtraParameters).WithTimeout(5000); } } catch (AdalUserMismatchException aume) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)aume, $"{Constants.AzureAuthenticator.AZURELOGERRORTAG} :AdalUserMismatchException", memberName, lineNumber, $"Returned User: {aume?.ReturnedUser} Requested User: {aume?.RequestedUser}", $"Error Code:: {aume?.ErrorCode}"); } } catch (AdalSilentTokenAcquisitionException astae) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)astae, $"AdalSilientTokenAquisitionException {astae?.ErrorCode}", memberName, lineNumber, astae.Data); } } catch (AdalClaimChallengeException acce) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)acce, $"AdalClaimsChallengeException:: Claims: {acce.Claims}", memberName, lineNumber, acce.Data, acce.Headers, acce.ServiceErrorCodes, acce.StatusCode); } throw; } catch (AdalServiceException ase) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)ase, $"{Constants.AzureAuthenticator.AZURELOGERRORTAG}: AdalServiceException::", memberName, lineNumber, ase.Data, ase.Headers, ase.ServiceErrorCodes, ase.StatusCode); } } catch (Exception e) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), e, e.Message, memberName, lineNumber, null); } } return(CacheToken.GetCacheToken(endpoint, results)); } else { throw new Exception("ERROR: Endpoint not Active, please make Endpoint Active and try again."); } }
public async Task <CacheToken> AcquireTokenSilentAsync(Endpoint endpoint, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { AuthenticationResult results = null; try { var authContext = new AuthenticationContext(endpoint.Authority); //fixes for security groups in iOS per //https://aka.ms/adal-net-ios-keychain-access #if iOS authContext.iOSKeychainSecurityGroup = endpoint.iOSKeychainSecurityGroup; #endif _logBuilder.Clear(); if (endpoint.EnableLogging) { LoggerCallbackHandler.LogCallback = AdalLog; LoggerCallbackHandler.PiiLoggingEnabled = true; } results = await authContext.AcquireTokenSilentAsync(endpoint.ResourceId, endpoint.ApplicationId); } catch (AdalUserMismatchException aume) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)aume, $"{Constants.AzureAuthenticator.AZURELOGERRORTAG} :AdalUserMismatchException", memberName, lineNumber, $"Returned User: {aume?.ReturnedUser} Requested User: {aume?.RequestedUser}", $"Error Code:: {aume?.ErrorCode}"); } } catch (AdalSilentTokenAcquisitionException astae) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)astae, $"AdalSilientTokenAquisitionException {astae?.ErrorCode}", memberName, lineNumber, astae.Data); } //get the token regularly since it's not in cache //per documentation: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/AcquireTokenSilentAsync-using-a-cached-token return(await AuthenticateEndpoint(endpoint)); } catch (AdalClaimChallengeException acce) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)acce, $"AdalClaimsChallengeException:: Claims: {acce.Claims}", memberName, lineNumber, acce.Data, acce.Headers, acce.ServiceErrorCodes, acce.StatusCode); } } catch (AdalServiceException ase) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), (System.Exception)ase, $"{Constants.AzureAuthenticator.AZURELOGERRORTAG}: AdalServiceException::", memberName, lineNumber, ase.Data, ase.Headers, ase.ServiceErrorCodes, ase.StatusCode); } } catch (Exception e) { if (endpoint.EnableLogging) { _loggingService.LogError(typeof(AzureAuthenticatorService), e, e.Message, memberName, lineNumber, null); } } return(CacheToken.GetCacheToken(endpoint, results)); }