title: System.SystemException: The trust relationship between this workstation and the primary domain failed
body: <p>we have software Tool on which we are trying to create a windows user as Service Account , to run some of the Services using that userService Account. In one of the machine, we have got the following exception which failed to create an UserAccount .</p>
  <p>Error :</p>
  <pre><code>2022/02/21 10:24:06.276|ERROR|Failed to create Service Account : System.SystemException: The trust relationship between this workstation and the primary domain failed.
  at System.Security.Principal.NTAccount.TranslateToSids(IdentityReferenceCollection sourceAccounts, Boolean&amp; someFailed)
  at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
  at System.Security.Principal.NTAccount.Translate(Type targetType)
  at InstallPackage.Models.ServiceAccount.ServiceAccountManager.IsServiceAccountExists(String name)
  at InstallPackage.Models.ServiceAccount.ServiceAccountCreateTask.&lt;OperationFunction&gt;d__6.MoveNext() |features|
  </code></pre>
  <p>The following is the piece of code related to User Creation.</p>
  <pre><code>public bool CreateServiceAccount(string userName)
  {
  var isUserCreated = false;
  //create service Account to install and run services
  bool userExists = IsServiceAccountExists(userName);
  if (userExists)
  {
  DeleteUser(userName, serviceCredentialsFile);
  }
  isUserCreated = CreateUser(userName, serviceCredentialsFile);
  return isUserCreated;
  }
  private bool IsServiceAccountExists(string name)
  {
  bool exists = false;
  try
  {
  NTAccount acct = new NTAccount(name);
  SecurityIdentifier id = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
  exists = id.IsAccountSid();
  }
  catch (IdentityNotMappedException ex)
  {
  _log.LogError(&quot;Invalid user account &quot; + ex.ToString());
  }
  catch (Exception ex)
  {
  _log.LogError(&quot;Error in verifying the service account exist: &quot; + ex);
  }
  return exists;
  }
  private bool CreateUser(string userName, string serviceCredentialsFile)
  {
  try
  {
  var encryptedPwd = string.Empty;
  string plainPassword = CommonHelper.GetRandomPassword();
  encryptedPwd = Crypto.ProtectStringV1(plainPassword);
  DirectoryEntry activeDirectory = new DirectoryEntry(&quot;WinNT://&quot; +
  Environment.MachineName + &quot;,computer&quot;);
  DirectoryEntry newUser = activeDirectory.Children.Add(userName, &quot;user&quot;);
  newUser.Invoke(&quot;SetPassword&quot;, new object[] { plainPassword });
  newUser.Invoke(&quot;Put&quot;, new object[] { &quot;Description&quot;, &quot;Service account for Windows Services&quot; });
  newUser.CommitChanges();
  DirectoryEntry grp;
  ServiceAccountStoreSettings serviceAccountCredentialDetails = new ServiceAccountStoreSettings
  {
  UserName = userName,
  EncryptedPassword = encryptedPwd
  };
  var protectedJsonData = Protect.ProtectSettingFile(JsonSerializer.Serialize(serviceAccountCredentialDetails));
  File.WriteAllText(serviceCredentialsFile, protectedJsonData);
  var UsersGroup = WindowsGroupPermissionsManager.GetGroupByName(
  WindowsGroupPermissionsManager.USERS_GROUP_NAME, WindowsGroupPermissionsManager.USERS_GROUP_DESC);
  // add ServiceUser to Group
  AddUserToGroups(activeDirectory, newUser,UsersGroup.Name);
  var principalContext = new PrincipalContext(ContextType.Machine);
  var performanceMonitorGroup = GroupPrincipal.FindByIdentity(principalContext, &quot;Performance Monitor Users&quot;);
  // add ServiceUser to Performance Monitor Users Group
  AddUserToGroups(activeDirectory ,newUser,performanceMonitorGroup.Name);
  var performanceLogUsersGroup = GroupPrincipal.FindByIdentity(principalContext, &quot;Performance Log Users&quot;);
  // add ServiceUser to Performance Log Users Group
  AddUserToGroups(activeDirectory ,newUser,performanceLogUsersGroup.Name);
  _log.LogInformation(&quot;Account Created Successfully&quot;);
  GrantLogonAsServiceRight(userName);
  return true;
  }
  catch (Exception ex)
  {
  _log.LogError(&quot;unable to create user Account&quot;, ex.ToString());
  return false;
  }
  }
  </code></pre>