title: Asp net core MVC Authorization with Active Directory
body: <p>In an Asp net core MVC application, I use Active Directory for automatic login like this :</p>
  
  <pre><code>this.user = UserPrincipal.FindByIdentity(this.context, Environment.UserName);
  </code></pre>
  
  <p>and I get groups of the user with this :</p>
  
  <pre><code>public List&lt;String&gt; GetUserGroups()
  {
     List&lt;String&gt; groups = new List&lt;String&gt;();
     foreach(GroupPrincipal gr in user.GetGroups())
     {
        groups.Add(gr.Name);
     }
     return groups;
  }
  </code></pre>
  
  <p>And I would like to implement Autorisation with this groups, something like that :</p>
  
  <pre><code>[Authorize(Roles ="Admin")]
  public IActionResult OnlyAdmin(){}
  </code></pre>
  
  <p>with something that link AD groups with authorization Roles or directly check authorization with AD groups if possible but I don't know how to do something like that. </p>
  
  <p><em>note : I haven't any login/logout pages, it's only automatic.</em></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Don't know exactly why or how but it finaly work whithout any code and only with the user login in the PC not the user specified in <code>this.user</code> but it's fine like that.</p>
  
  <p>But now I get a 404 error when I'm trying to access a denied page, why it's not a 401 or 403 error ? How can I redirect a denied access to a custom error page ? </p>