Beispiel #1
0
        private async Task GetAuthenticationContext(string baseUri, string crmUrl, string applicationId, string applicationkey, string aadId, Func <AuthenticationContext, Task> action)
        {
            var strKey = GenerateKeyString(baseUri, crmUrl, applicationId, applicationkey, aadId);

            if (!_authenticationContexts.TryGetValue(strKey, out AuthenticationContextContainer contextContainer))
            {
                SharePool <AuthenticationContext> contextPool = new SharePool <AuthenticationContext>("CrmS2S",
                                                                                                      null,
                                                                                                      (authcContext) =>
                {
                    return(true);
                }
                                                                                                      ,
                                                                                                      (authcContext) =>
                {
                },
                                                                                                      async() =>
                {
                    AuthenticationContext newContext = await CreateAuthenticationContext(baseUri, crmUrl, aadId);
                    return(await Task.FromResult(newContext));
                }
                                                                                                      ,
                                                                                                      async(authcContext) =>
                {
                    return(await Task.FromResult(true));
                }
                                                                                                      ,
                                                                                                      async(authcContext) =>
                {
                    await Task.FromResult(0);
                }
                                                                                                      ,
                                                                                                      _poolLimit
                                                                                                      );

                contextContainer = new AuthenticationContextContainer()
                {
                    ContextPool = contextPool, LastTime = DateTime.UtcNow
                };



                lock (_authenticationContexts)
                {
                    if (_authenticationContexts.Count > _limit)
                    {
                        var deleteItem = (from item in _authenticationContexts
                                          orderby item.Value.LastTime
                                          select item
                                          ).FirstOrDefault();
                        if (deleteItem.Key != null)
                        {
                            _authenticationContexts.Remove(deleteItem.Key);
                        }
                    }

                    _authenticationContexts[strKey] = contextContainer;
                }
            }

            AuthenticationContext context = null;

            context = await contextContainer.ContextPool.GetAsync();
            await action(context);
        }
        private async Task GetAdfsAuth(string adfsUrl, string clientId, string clientSecret, string crmUrl, string userName, string password, Func <AdfsAuth, Task> action)
        {
            var strKey = GenerateKeyString(adfsUrl, clientId, clientSecret, crmUrl, userName, password);

            if (!_adfsAuthContexts.TryGetValue(strKey, out AdfsAuthWrapperContainer wrapperContainer))
            {
                SharePool <AdfsAuthWrapper> wrapperPool = new SharePool <AdfsAuthWrapper>("Adfs",
                                                                                          () =>
                {
                    return(null);
                },
                                                                                          (wrapper) =>
                {
                    return(true);
                }
                                                                                          ,
                                                                                          (wrapper) =>
                {
                },
                                                                                          async() =>
                {
                    var auth = await AdfsHelper.GetAdfsAuthDirect(adfsUrl, $"{crmUrl}/api/data", clientId, clientSecret, userName, password);
                    AdfsAuthWrapper wrapper = new AdfsAuthWrapper()
                    {
                        AdfsAuth      = auth,
                        AdfsParameter = new AdfsParameter()
                        {
                            AdfsUrl = adfsUrl, ClientId = clientId, CrmUrl = crmUrl, ClientSecret = clientSecret, UserName = userName, Password = password
                        },
                        CreateTime      = DateTime.UtcNow,
                        TokenCreateTime = DateTime.UtcNow
                    };
                    return(wrapper);
                }
                                                                                          ,
                                                                                          async(wrapper) =>
                {
                    if ((DateTime.UtcNow - wrapper.CreateTime).TotalSeconds > _refreashTokenTimeout - 100)
                    {
                        return(await Task.FromResult(false));
                    }
                    return(await Task.FromResult(true));
                }
                                                                                          ,
                                                                                          async(wrapper) =>
                {
                    await Task.FromResult(0);
                },
                                                                                          _poolLimit
                                                                                          );

                wrapperContainer = new AdfsAuthWrapperContainer()
                {
                    ContextPool = wrapperPool, LastTime = DateTime.UtcNow
                };



                lock (_adfsAuthContexts)
                {
                    if (_adfsAuthContexts.Count > _limit)
                    {
                        var deleteItem = (from item in _adfsAuthContexts
                                          orderby item.Value.LastTime
                                          select item
                                          ).FirstOrDefault();
                        if (deleteItem.Key != null)
                        {
                            _adfsAuthContexts.Remove(deleteItem.Key);
                        }
                    }
                    _adfsAuthContexts[strKey] = wrapperContainer;
                }
            }

            AdfsAuthWrapper adfsAuthWrapper = null;

            AdfsAuth adfsAuth = null;

            adfsAuthWrapper = await wrapperContainer.ContextPool.GetAsync();

            if ((DateTime.UtcNow - adfsAuthWrapper.TokenCreateTime).TotalSeconds > adfsAuthWrapper.AdfsAuth.Expires - 20)
            {
                adfsAuth = await AdfsHelper.RefreshToken(adfsAuthWrapper.AdfsParameter.AdfsUrl, clientId, clientSecret, adfsAuthWrapper.AdfsAuth.RefreshToken);

                adfsAuthWrapper.AdfsAuth        = adfsAuth;
                adfsAuthWrapper.TokenCreateTime = DateTime.UtcNow;
            }
            else
            {
                adfsAuth = adfsAuthWrapper.AdfsAuth;
            }

            await action(adfsAuth);
        }