title: Hosting environment.impersonate is not woking with System.DirectoryServices.AccountManagement on the server
body: <p>Hi I want to retrieve the groups belonging to logged in user I tried following code but it is not working on the server.</p>
  
  <pre><code>   using (System.Web.Hosting.HostingEnvironment.Impersonate())
     {  
      string userName = HttpContext.Current.User.Identity.Name;
             PrincipalContext domain = new PrincipalContext(ContextType.Domain);
      UserPrincipal User = new UserPrincipal(domain);
      User = UserPrincipal.FindByIdentity(domain, userName);
      var strUnAuthorised = "";
      if (User != null)
      {
          PrincipalSearchResult&lt;Principal&gt; gp = User.GetGroups();
          foreach (Principal p in gp)
          {
              if (p.DisplayName == "xyz")
              {
                  IsInGroup = true;
                  break;
              }
          }
      }
  }
  </code></pre>
  
  <p>after this when I am trying to read one xml file from the server I am getting not authorised error. please help me I am struggling alot</p>