Пример #1
0
 internal void ResetAll()
 {
     ResetTokenCache();
     _appSettings = null;
     _authContext = null;
     Trace.TraceInformation("Reset complete");
 }
        public static S2SAppSettings GetAppSettings()
        {
            Trace.TraceInformation("GetAppSettings with ConfigurationManager");
            var settings = new S2SAppSettings()
            {
                ClientId     = ConfigurationManager.AppSettings[_settingClientId],
                ClientSecret = ConfigurationManager.AppSettings[_settingClientSecret],
                Resource     = ConfigurationManager.AppSettings[_settingResource],
                TenantId     = ConfigurationManager.AppSettings[_settingTenantId]
            };

            return(settings);
        }
Пример #3
0
        public CrmContext(S2SAppSettings settings, Func <S2SAppSettings, string> getToken = null)
        {
            if (_appSettings == null || !_appSettings.Equals(settings))
            {
                settings.AssertAppSettings();
            }
            _appSettings = settings;

            _resource = settings.Resource;
            Trace.TraceInformation($"CrmContext set instance: {_resource}");

            _getToken = getToken == null ? GetDefaultToken : getToken;
        }
 public static bool LoadConfigAppSettings(S2SAppSettings appSettings)
 {
     try
     {
         ConfigurationManager.AppSettings[_settingClientId]     = appSettings.ClientId;
         ConfigurationManager.AppSettings[_settingClientSecret] = appSettings.ClientSecret;
         ConfigurationManager.AppSettings[_settingResource]     = appSettings.Resource;
         ConfigurationManager.AppSettings[_settingTenantId]     = appSettings.TenantId;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #5
0
 public static bool LoadConfigAppSettings(S2SAppSettings appSettings)
 {
     Trace.TraceInformation("Start load/set with ConfigurationManager of app settings");
     try
     {
         ConfigurationManager.AppSettings[_settingClientId]     = appSettings.ClientId;
         ConfigurationManager.AppSettings[_settingClientSecret] = appSettings.ClientSecret;
         ConfigurationManager.AppSettings[_settingResource]     = appSettings.Resource;
         ConfigurationManager.AppSettings[_settingTenantId]     = appSettings.TenantId;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #6
0
        private string GetDefaultToken(S2SAppSettings settings)
        {
            var authority = $"{AadInstance}{settings.TenantId}";

            if (_authContext == null || !_authContext.Authority.Contains(authority))
            {
                _authContext = new AuthenticationContext(authority);
                Trace.TraceInformation($"CrmContext set auth context: {authority}");
            }
            var result = _authContext.AcquireToken(settings.Resource,
                                                   new ClientCredential(settings.ClientId, settings.ClientSecret));

            Trace.TraceInformation($"ADAL AcquireToken complete");

            return(result.AccessToken);
        }
Пример #7
0
 public virtual bool Equals(S2SAppSettings settings)
 {
     if (!ClientId.Equals(settings.ClientId))
     {
         return(false);
     }
     if (!ClientSecret.Equals(settings.ClientSecret))
     {
         return(false);
     }
     if (!Resource.Equals(settings.Resource))
     {
         return(false);
     }
     if (!TenantId.Equals(settings.TenantId))
     {
         return(false);
     }
     return(true);
 }
 private static bool ValidateAppSettings(S2SAppSettings appSettings)
 {
     if (string.IsNullOrEmpty(appSettings.ClientId))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(appSettings.ClientSecret))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(appSettings.Resource))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(appSettings.TenantId))
     {
         return(false);
     }
     return(true);
 }
Пример #9
0
 public static CrmContext Create(S2SAppSettings settings)
 {
     return(new CrmContext(settings));
 }