Ejemplo n.º 1
0
        public async Task <SqlConnection> OpenConnection(Instance instance)
        {
            if (instance == null)
            {
                logger.Error("ConnectionManager.OpenConnection: get null instance");
                return(null);
            }

            Impersonation       impersonation = null;
            ImpersonationResult impResult     = null;

            connection = new SqlConnection(BuildConnectionString(instance));


            if (instance.Authentication == AuthenticationType.Windows)
            {
                impersonation = new Impersonation();
                impResult     = impersonation.Impersonate(instance.Login, null, instance.Password);//тут

                if (impResult.HasError)
                {
                    logger.Error("Imperosnation error: ConnectionManager.OpenConnection: Instance id =" + instance.Id);
                }
                else
                {
                    logger.Debug("Impersonation oK instance id =" + instance.Id);
                }

                return(null);
            }


            try
            {
                //ConfigureAwait(true); to return to the same context to do undo imersonation for the same thread
                await connection.OpenAsync().ConfigureAwait(true);
            }
            catch (Exception e)
            {
                logger.Error("ConnectionManager.OpenConnection: Error open connection  instance=" + instance.InstanceName, e);
                return(null);
            }
            finally
            {
                if (impResult != null && impersonation != null)
                {
                    if (!impResult.HasError)
                    {
                        impersonation.UndoImpersonation(impResult.User);
                    }
                }
            }

            return(connection);
        }
Ejemplo n.º 2
0
        public static async Task <string> Check(string servername, string instanceName, string login, string pswd, bool windowsoauth)
        {
            String connString = BuildConnectionString(servername, instanceName, login, pswd, windowsoauth);

            Impersonation       impersonation = null;
            ImpersonationResult impResult     = null;

            SqlConnection connection = new SqlConnection(connString);

            if (windowsoauth)
            {
                impersonation = new Impersonation();
                impResult     = impersonation.Impersonate(login, null, pswd);

                if (impResult.HasError)
                {
                    return("Windows impersonation error!");
                }
            }


            try
            {
                await connection.OpenAsync().ConfigureAwait(true);

                connection.Close();
            }
            catch (Exception e)
            {
                return("Open connection error!\n" + e.Message);
            }
            finally
            {
                if (impResult != null && impersonation != null)
                {
                    if (!impResult.HasError)
                    {
                        impersonation.UndoImpersonation(impResult.User);
                    }
                }
            }


            return("Ok!");
        }