Ejemplo n.º 1
0
 internal string GetValidAccessToken()
 {
     if (this.refreshableToken && DateTimeOffset.Now > this.reAcquireOn)
     {
         AadTokenHolder aadTokenHolder = AadAuthenticator.ReAcquireToken(this.refreshToken, this.authParams, this.dataSource, this.useAdalCache);
         this.SetValues(aadTokenHolder.accessToken, aadTokenHolder.refreshToken, aadTokenHolder.expiresOn, aadTokenHolder.authParams, aadTokenHolder.dataSource, aadTokenHolder.useAdalCache, true);
     }
     return(this.accessToken);
 }
Ejemplo n.º 2
0
 internal HttpStream(Uri dataSourceUri, bool acceptCompressedResponses, DataType desiredRequestType, DataType desiredResponseType, CookieContainer cookieContainer, int timeoutMs, ConnectionInfo connectionInfo) : base(desiredRequestType, desiredResponseType)
 {
     try
     {
         this.webSite           = dataSourceUri;
         this.hasASAzureHeaders = false;
         this.connectionInfo    = connectionInfo;
         if (connectionInfo.IsInternalASAzure)
         {
             this.hasASAzureHeaders = true;
         }
         if (connectionInfo.IsAsAzure)
         {
             this.aadTokenHolder    = connectionInfo.AadTokenHolder;
             this.xmlaServerHeader  = connectionInfo.AsAzureServerName;
             this.hasASAzureHeaders = true;
         }
         else if (connectionInfo.IntegratedSecurity == IntegratedSecurity.Federated)
         {
             if (string.IsNullOrEmpty(connectionInfo.IdentityProvider) || string.Compare(connectionInfo.IdentityProvider, "MsoID", CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0)
             {
                 throw new NotSupportedException(XmlaSR.ConnectionString_InvalidIdentityProviderForIntegratedSecurityFederated);
             }
             this.authorizationHeader = "MsoID " + MsoIDAuthenticationProvider.Instance.Authenticate(connectionInfo.UserID, connectionInfo.Password);
         }
         else if (connectionInfo.UserID == null)
         {
             this.logonWindowsIdentity      = WindowsIdentity.GetCurrent();
             this.credentials               = CredentialCache.DefaultCredentials;
             this.connectionSecureGroupName = this.logonWindowsIdentity.User.ToString();
         }
         else
         {
             this.credentials = new NetworkCredential(connectionInfo.UserID, connectionInfo.Password);
             SHA1Managed sHA1Managed = new SHA1Managed();
             byte[]      bytes       = sHA1Managed.ComputeHash(Encoding.UTF8.GetBytes(connectionInfo.Password));
             this.connectionSecureGroupName = connectionInfo.UserID.Replace(";", ";;") + ";:" + Encoding.Default.GetString(bytes).Replace(";", ";;");
         }
         if (!string.IsNullOrEmpty(connectionInfo.ClientCertificateThumbprint))
         {
             this.clientCertificate = CertUtils.LoadCertificateByThumbprint(connectionInfo.ClientCertificateThumbprint, true);
         }
         this.acceptCompressedResponses = acceptCompressedResponses;
         this.applicationName           = connectionInfo.ApplicationName;
         this.cookieContainer           = cookieContainer;
         this.timeoutMs            = timeoutMs;
         this.routingTokenAccessor = new HttpStream.RoutingTokenAccessor(connectionInfo);
         this.allowAutoRedirect    = connectionInfo.AllowAutoRedirect;
     }
     catch (UriFormatException innerException)
     {
         throw new XmlaStreamException(innerException);
     }
 }
        internal static AadTokenHolder ReAcquireToken(string refreshToken, AadAuthParams authParams, string dataSource, bool useAdalCache)
        {
            AadTokenHolder result;

            using (new AdalRuntimeLibrary.Usage())
            {
                AdalRuntimeLibrary instance = AdalRuntimeLibrary.Instance;
                object             obj      = useAdalCache ? AadAuthenticator.CreateAuthenticationContextWithCache(authParams, dataSource) : Activator.CreateInstance(instance.AuthenticationContextT, new object[]
                {
                    authParams.Authority
                });
                object obj2 = instance.AcquireTokenByRefreshTokenM.Invoke(obj, new string[]
                {
                    refreshToken,
                    authParams.ApplicationId
                });
                result = new AadTokenHolder((string)instance.AccessTokenP.GetValue(obj2, null), (string)instance.RefreshTokenP.GetValue(obj2, null), (DateTimeOffset)instance.ExpiresOnP.GetValue(obj2, null), authParams, dataSource, useAdalCache);
            }
            return(result);
        }
        internal static AadTokenHolder AcquireToken(Uri dataSourceUri, string dataSource, string identityProvider, string userId, string password, bool useAdalCache)
        {
            if (userId == null && password != null)
            {
                return(new AadTokenHolder(password));
            }
            AadAuthParams  aadAuthParams = AadAuthParams.FindMatchingAuthParams(identityProvider, dataSourceUri);
            string         text          = string.Format("https://{0}", dataSourceUri.Host);
            AadTokenHolder result;

            using (new AdalRuntimeLibrary.Usage())
            {
                AdalRuntimeLibrary instance = AdalRuntimeLibrary.Instance;
                object             obj;
                if (useAdalCache)
                {
                    obj = AadAuthenticator.CreateAuthenticationContextWithCache(aadAuthParams, dataSource);
                    AadAuthParams aadAuthParams2;
                    if (aadAuthParams.IsCommonTenant && AadAuthenticator.TryToUpdateTenant(aadAuthParams, obj, userId, out aadAuthParams2))
                    {
                        aadAuthParams = aadAuthParams2;
                        obj           = AadAuthenticator.CreateAuthenticationContextWithCache(aadAuthParams, dataSource);
                    }
                }
                else
                {
                    obj = Activator.CreateInstance(instance.AuthenticationContextT, new object[]
                    {
                        aadAuthParams.Authority
                    });
                }
                object obj3;
                if (userId != null && password != null)
                {
                    object obj2 = Activator.CreateInstance(instance.UserCredentialT, new object[]
                    {
                        userId,
                        password
                    });
                    obj3 = instance.AcquireTokenCredentialsM.Invoke(obj, new object[]
                    {
                        text,
                        aadAuthParams.ApplicationId,
                        obj2
                    });
                }
                else if (userId != null)
                {
                    object obj4 = Activator.CreateInstance(instance.UserIdentifierT, new object[]
                    {
                        userId,
                        instance.OptionalDisplayableIdV
                    });
                    obj3 = instance.AcquireTokenUserIdM.Invoke(obj, new object[]
                    {
                        text,
                        aadAuthParams.ApplicationId,
                        AadAuthenticator.RedirectUri,
                        instance.PromptBehaviorAutoV,
                        obj4
                    });
                }
                else
                {
                    obj3 = instance.AcquireTokenM.Invoke(obj, new object[]
                    {
                        text,
                        aadAuthParams.ApplicationId,
                        AadAuthenticator.RedirectUri,
                        instance.PromptBehaviorAutoV
                    });
                }
                result = new AadTokenHolder((string)instance.AccessTokenP.GetValue(obj3, null), (string)instance.RefreshTokenP.GetValue(obj3, null), (DateTimeOffset)instance.ExpiresOnP.GetValue(obj3, null), aadAuthParams, dataSource, useAdalCache);
            }
            return(result);
        }
        internal static Uri ResolveActualClusterUri(Uri dataSourceUri, string asAzureServerName, AadTokenHolder aadTokenHolder, ref TimeoutUtils.TimeLeft timeLeft, TimeoutUtils.OnTimoutAction timeoutAction)
        {
            Uri uri = ASAzureUtility.ClustersCache.GetCluster(dataSourceUri, asAzureServerName);

            if (uri != null)
            {
                return(uri);
            }
            try
            {
                using (new TimeoutUtils.TimeRestrictedMonitor(timeLeft, timeoutAction))
                {
                    Uri uri2 = new UriBuilder(dataSourceUri)
                    {
                        Path = "/webapi/clusterResolve"
                    }.Uri;
                    ASAzureUtility.NameResolutionRequest requestObject = new ASAzureUtility.NameResolutionRequest
                    {
                        ServerName = asAzureServerName
                    };
                    ASAzureUtility.NameResolutionResult nameResolutionResult = ASAzureUtility.PostHttpJsonData <ASAzureUtility.NameResolutionRequest, ASAzureUtility.NameResolutionResult>(uri2, requestObject, aadTokenHolder.GetValidAccessToken(), timeLeft.TimeMs);
                    uri = new UriBuilder(dataSourceUri)
                    {
                        Host = nameResolutionResult.ClusterFqdn
                    }.Uri;
                    ASAzureUtility.ClustersCache.AddCluster(dataSourceUri, asAzureServerName, uri);
                }
            }
            catch (WebException ex)
            {
                ASAzureUtility.ThrowConnectionException(ex);
            }
            return(uri);
        }