title: Query/Change SPNs on Windows Domain without SetSPN
body: <p>Has anyone had any luck with querying/changing SPNs on a Windows domain? Most of the hits on Google are SQL related: I can't find any information on how to do this myself. The most important things would be to query to SPN configuration and check for duplicates.</p>
  
  <p>According to Arnout I made the following code:</p>
  
  <pre><code>static void Main(string[] args) {
      ValidateSPN("K2Server/jonathand-vpc:5252");
  }
  
  static void ValidateSPN(string spn) {
      const string queryFormat = "(ServicePrincipalName={0})";
      using (Domain localDomain = Domain.GetCurrentDomain()) {
          using (DirectorySearcher search = new DirectorySearcher(localDomain.GetDirectoryEntry())) {
              search.Filter = string.Format(queryFormat, spn);
              search.SearchScope = SearchScope.Subtree;
              SearchResultCollection collection = search.FindAll();
              if (collection.Count &gt; 1)
                  throw new Exception("Duplicate SPNs found.");
              else if (collection.Count == 0)
                  throw new Exception("No such SPN");
          }
      }
  }
  </code></pre>