/// <summary> /// Acquires an OAuth 2.0 access token from Azure AD for use with the Records365 Connector API. /// </summary> /// <param name="settings"></param> /// <param name="useTokenCache"></param> /// <returns></returns> public async Task <AuthenticationResult> AcquireTokenAsync(AuthenticationHelperSettings settings, bool useTokenCache = true) { ValidationHelper.ArgumentNotNull(settings, nameof(settings)); ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.AuthenticationResource, nameof(settings.AuthenticationResource)); ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.ClientId, nameof(settings.ClientId)); var authority = GetAuthority(settings); // sign in & get an authentication token... var authenticationContext = useTokenCache // by default an internal token cache is used ? new AuthenticationContext(authority) // pass a null token cache so that the token must be retrieved from the authority : new AuthenticationContext(authority, null); #if NETSTANDARD2_0 //NOTE: The following call may throw a PlatformNotSupported exception that occurs the first time a token is acquired. //However, the exception will not raise beyond this point, and the call will automatically retry and resolved a token on the second attempt. //This is due to the first (throwing) attempt actually acquiring a token, storing it in the AuthenticationContext token cache, and then throwing an exception regardless. //Later attempts simply use the cached token and thus avoid the issue. While undocumented, it is believed that this is the intended functionality. var aadAuthenticationResult = await authenticationContext.AcquireTokenAsync(settings.AuthenticationResource, new ClientCredential(settings.ClientId, SecureStringToString(settings.ClientSecret))).ConfigureAwait(false); #else var aadAuthenticationResult = await authenticationContext.AcquireTokenAsync(settings.AuthenticationResource, new ClientCredential(settings.ClientId, new SecureClientSecret(settings.ClientSecret))).ConfigureAwait(false); #endif return(new AuthenticationResult { AccessTokenType = aadAuthenticationResult.AccessTokenType, AccessToken = aadAuthenticationResult.AccessToken }); }
/// <summary> /// Creates an IApiClient. /// </summary> /// <param name="settings"></param> /// <returns></returns> public IApiClient CreateApiClient(ApiClientFactorySettings settings) { ValidationHelper.ArgumentNotNull(settings, nameof(settings)); ValidationHelper.ArgumentNotNullOrWhiteSpace(settings.ConnectorApiUrl, nameof(settings.ConnectorApiUrl)); // get the api client generated by AutoRest from the swagger as a singleton object return(GetApiClient(settings)); }
/// <summary> /// Adds or updates a ConnectorConfigModel. /// Notifications will be polled and processed For all Enabled ConnectorConfigModels added. /// </summary> /// <param name="connectorConfig"></param> public void UpsertConnectorConfigModel(ConnectorConfigModel connectorConfig) { ValidationHelper.ArgumentNotNull(connectorConfig, nameof(connectorConfig)); var shouldRefresh = false; _connectors.AddOrUpdate(connectorConfig.IdAsGuid, // Added a new Connector (key) => { // Only need to refresh the tasks if the new Connector is enabled shouldRefresh = connectorConfig.Status == ConnectorConfigStatus.Enabled; return(connectorConfig); }, // Updating an existing connector (key, value) => { // Always refresh in case the connector has changed shouldRefresh = true; return(connectorConfig); }); if (shouldRefresh) { Refresh(); } }
/// <summary> /// Allows an initial set of connectors to be defined, e.g. from persistent storage /// </summary> /// <param name="initialConnectors"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task Run(IEnumerable <ConnectorConfigModel> initialConnectors, CancellationToken cancellationToken) { ValidationHelper.ArgumentNotNull(initialConnectors, nameof(initialConnectors)); // NotificationTaskRunner should be a singleton - This is not thread safe _connectors = new ConcurrentDictionary <Guid, ConnectorConfigModel>(initialConnectors.Select(x => new KeyValuePair <Guid, ConnectorConfigModel>(x.IdAsGuid, x))); await Run(cancellationToken).ConfigureAwait(false); }
public int Delete <T>(object pkValue) where T : class { ValidationHelper.ArgumentNotNull(pkValue, "删除主键"); string pkName = AttributeHelper.GetEntityPrimaryKey <T>(); StringBuilder sb = new StringBuilder($"Delete From {typeof(T).GetMappingAttributeName()} Where {pkName} = ?{pkName}"); int i = ExecuteNonQuery(sb.ToString(), new MySqlParameter(pkName, pkValue)); return(i); }
/// <summary> /// 创瑞短信平台API /// </summary> /// <param name="accessKey">accesskey</param> /// <param name="secret">secret</param> /// <param name="sign">sign</param> public SMS_ChuangRui_V2(string accessKey, string secret, string sign) { ValidationHelper.ArgumentNotNull(accessKey, "创瑞accessKey"); ValidationHelper.ArgumentNotNull(secret, "创瑞secret"); ValidationHelper.ArgumentNotNull(sign, "创瑞sign"); this.accessKey = accessKey; this.secret = secret; this.sign = sign; }
public static string GetValueOrDefault(this IList <MetaDataModel> metaDataList, string name) { ValidationHelper.ArgumentNotNull(metaDataList, nameof(metaDataList)); ValidationHelper.ArgumentNotNullOrEmpty(name, nameof(name)); var existing = metaDataList.SingleOrDefault(x => x.Name == name); if (existing == null) { return(string.Empty); } return(existing.Value); }
public T FindEntity <T>(object keyValue) where T : class { ValidationHelper.ArgumentNotNull(keyValue, "FindEntity:查询关键字"); StringBuilder sb = new StringBuilder(); sb.Append($"SELECT * FROM {typeof(T).GetMappingAttributeName()} WHERE ").Append(AttributeHelper.GetEntityPrimaryKey <T>()).Append("=?ID"); return(FindList <T>(sb.ToString(), new MySqlParameter("?ID", keyValue)).FirstOrDefault()); //T model = Activator.CreateInstance<T>(); //DataTable dt = FindTable(sb.ToString(), new MySqlParameter("?ID", keyValue)); //if (dt != null && dt.Rows.Count > 0) // return DataConvertHelper.DataRowToModel<T>(dt.Rows[0]); //return model; }
public static IList <MetaDataModel> AddOrUpdate(this IList <MetaDataModel> metaDataList, string name, string type, string value) { ValidationHelper.ArgumentNotNull(metaDataList, nameof(metaDataList)); ValidationHelper.ArgumentNotNullOrEmpty(name, nameof(name)); // null or empty value is permitted var existing = metaDataList.SingleOrDefault(x => x.Name == name); if (existing != null) { existing.Type = type; existing.Value = value; } else { metaDataList.Add(new MetaDataModel { Name = name, Type = type, Value = value }); } return(metaDataList); }
private static string FormatWith(this string format, IFormatProvider provider, params object[] args) { ValidationHelper.ArgumentNotNull(format, "format"); return(string.Format(provider, format, args)); }
/* * TypeOf() 和GetType()的区别: * (1)TypeOf():运算符 得到一个Class的Type * (2)GetType():得到一个Class的实例的Type */ public static bool IsNullableType(Type t) { ValidationHelper.ArgumentNotNull(t, "Type"); return(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>)); }