internal void InsertLanguageData(Workspace workspace, ResourceType resourceType) { fxLanguages languages = new fxLanguages(); fxLanguage language = languages.Choose(); AstoriaTestLog.TraceInfo("Language Data: " + language.GetType().Name); // do them one at a time so that if one fails, we know which using (SqlConnection sqlConn = new SqlConnection(this.MachineConnectionString)) { sqlConn.Open(); for (int i = 0; i < 5; i++) { using (SqlCommand sqlCmd = sqlConn.CreateCommand()) { sqlCmd.CommandTimeout = 0; sqlCmd.CommandText = GetInsertString(workspace, resourceType, language, TestUtil.Random); try { sqlCmd.ExecuteNonQuery(); } catch (SqlException error) { AstoriaTestLog.WriteLineIgnore("Error while executing: " + sqlCmd.CommandText); throw; } } } } }
private void DeleteDatabaseFiles() { try { IOUtil.EnsureFileDeleted(Path.Combine(this.DestinationFolder, this.DatabaseName + ".mdf")); } catch (Exception e) { AstoriaTestLog.TraceInfo("Database cleanup failed for " + this.DestinationFolder + this.DatabaseName + ".mdf"); } try { IOUtil.EnsureFileDeleted(Path.Combine(this.DestinationFolder, this.DatabaseName + ".ldf")); } catch (Exception e) { AstoriaTestLog.TraceInfo("Database cleanup failed for " + this.DestinationFolder + this.DatabaseName + ".mdf"); } }
/// <summary> /// Performs the specified <paramref name="action"/> impersonating the given account /// when <paramref name="impersonate"/> is true. /// </summary> public static void DoAction(String userName, String domainName, String password, bool impersonate, Action action) { if (impersonate) { TestUtil.CheckArgumentNotNull(userName, "userName"); TestUtil.CheckArgumentNotNull(domainName, "domainName"); TestUtil.CheckArgumentNotNull(password, "password"); IntPtr tokenHandle = IntPtr.Zero; IntPtr dupeTokenHandle = IntPtr.Zero; try { const int LOGON32_PROVIDER_DEFAULT = 0; //This parameter causes LogonUser to create a primary token. const int LOGON32_LOGON_INTERACTIVE = 2; const int SecurityImpersonation = 2; // Call LogonUser to obtain a handle to an access token. bool returnValue = LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle); AstoriaTestLog.TraceInfo("LogonUser called for user " + domainName + "\\" + userName); if (false == returnValue) { int ret = Marshal.GetLastWin32Error(); AstoriaTestLog.TraceInfo(String.Format("LogonUser failed with error code : {0}", ret)); AstoriaTestLog.TraceInfo(String.Format("Error: [{0}] {1}\n", ret, GetErrorMessage(ret))); int errorCode = 0x5; //ERROR_ACCESS_DENIED throw new System.ComponentModel.Win32Exception(errorCode); } AstoriaTestLog.TraceInfo("Value of Windows NT token: " + tokenHandle); bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle); if (false == retVal) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } // The token that is passed to the following constructor must // be a primary token in order to use it for impersonation. using (WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle)) using (WindowsImpersonationContext impersonatedUser = newId.Impersonate()) { action.Invoke(); // Stop impersonating the user. impersonatedUser.Undo(); } } catch (Exception ex) { AstoriaTestLog.TraceInfo("Exception occurred. " + ex.Message); } finally { // Free the tokens. if (tokenHandle != IntPtr.Zero) { CloseHandle(tokenHandle); } if (dupeTokenHandle != IntPtr.Zero) { CloseHandle(dupeTokenHandle); } } } else { action.Invoke(); } }