title: Can I bring Active Directory operations under transaction using IEnlistmentNotification
body: <p>I want to create user in database and in Active Directory. I'm afraid of having partial commit on database or Active Directory, so searching is there any code that brings AD under transaction. I got some details about <code>IEnlistmentNotification</code> and tried it on AD but it is not working. Even in some exception AD record are not rolled back.</p>
  
  <p>My code runs without any problem but I tried to throw some exception to rollback the record in AD but not working as expected.</p>
  
  <p>My create user in AD : </p>
  
  <pre><code>public class CreateUser : IEnlistmentNotification
  {
      private ADUser _adUser;
  
      public CreateUser(ADUser adUser)
      {
          _adUser = adUser;
  
          if (Transaction.Current != null)
          {
              Transaction.Current.EnlistVolatile(this, EnlistmentOptions.None);
          }
      }
  
      public bool CreateADUser()
      {
          try
          {
              using (var context = new PrincipalContext(ContextType.Domain, "xxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx", "xxxxxxxxxxxxxxxx"))
              {
                  using (var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName,_adUser.UserName))
                  {
                      if (userPrincipal.IsAccountLockedOut())
                      {
                          userPrincipal.UnlockAccount();
                      }
  
                      userPrincipal.SetPassword(_adUser.Password);
                      userPrincipal.Save();
                  }
              }
  
              return true;
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
      }
  
      public void Prepare(PreparingEnlistment preparingEnlistment)
      {
          preparingEnlistment.Prepared();
          preparingEnlistment.ForceRollback();
      }
  
      public void Commit(Enlistment enlistment)
      {
          enlistment.Done();
      }
  
      public void Rollback(Enlistment enlistment)
      {
          try
          {
              // Do something
          }
          finally
          {
              enlistment.Done();
          }
      }
  
      public void InDoubt(Enlistment enlistment)
      {
          enlistment.Done();
      }
  }
  </code></pre>
  
  <p>My main program :</p>
  
  <pre><code>static void Main(string[] args)
  {
      try
      {
          Guid guid = Guid.NewGuid();
  
          using (TransactionScope scope = new TransactionScope())
          {
              var adUser = new CreateUser(new ADUser
                  {
                      UserName = "newmancroos",
                      Password = "Testpass_7",
                      FirstName = "Newman",
                      LastName = "Croos"
                  });
              adUser.CreateADUser();
  
              var userContext = new UserContext();
              var user = new User
                  {
                      Id = 20,
                      FirstName = "Newman7",
                      LastName = "Croos7",
                      Address = "test Address7"
                  };
              userContext.Users.Add(user);
              userContext.SaveChanges();
  
              throw new Exception("Test Exception");
              scope.Complete();
          }
      }
      catch (TransactionException ex)
      {
          Console.WriteLine(ex);
      }
      catch (Exception ex)
      {
          Console.WriteLine("Error occurred " + ex.Message);
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>DB changes got rolled back, but not AD.</p>
  
  <p>Thank you in advance for your help</p>