title: ASP.NET Core policy base Authorize with RequireUser with string array
body: <p>I am creating policy base authorization and would like to allow multiple users in one policy to access webpage.</p>
  
  <p>I created policy like shown below in start up file. Question, How can I use multiple usernames in one policy? I looked at the method for<code>.RequireUserName</code>, it is only accepting string username.</p>
  
  <p>Policy name <code>AdminServiceAccount</code> is mostly I am interested in to add multiple users. If I use param <code>.RequireUserName("DOMAIN\\USER1,DOMAIN\\USER2")</code> will it work? I don't think so, but wanted to check if there is an alternative way.</p>
  
  <pre><code>services.AddAuthorization(
                  option =&gt;
                  {
                      option.AddPolicy("Admin", policy =&gt; policy.RequireRole("Domain\\GroupName"));
                      option.AddPolicy("SuperAdminUser", policy =&gt; policy.RequireUserName("DOMAIN\\SuperAdminUser"));
                      option.AddPolicy("AdminServiceAccount", policy =&gt; policy.RequireUserName("DOMAIN\\USER1"));
                  }
          );
  </code></pre>
  
  <p><strong>UPDATE 1:</strong>
  <a href="https://i.stack.imgur.com/ymmKi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ymmKi.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>So in my Controller, I added <code>[Authorize(Policy = "UserNamesPolicy")]</code> as show below:</p>
  
  <pre><code>[Authorize(Policy = "UserNamesPolicy")]
  public class ServersController : Controller
  {
      private readonly ServerMatrixDbContext _context;
  
      public ServersController(ServerMatrixDbContext context)
      {
          _context = context;    
      }
  
      // GET: Servers
      public async Task&lt;IActionResult&gt; Index()
      {
          // Some code here
          return View();
      }
  }
  </code></pre>
  
  <p>Here is my startup file:</p>
  
  <pre><code>services.AddAuthorization(
                  option =&gt;
                  {
                      option.AddPolicy("UserNamesPolicy",
                                            policy =&gt; policy.Requirements.Add(new UserNamesRequirement("DOMAIN\\USER1", "DOMAIN\\USER2"))
                      );
                  }
          );
  services.AddSingleton&lt;IAuthorizationHandler, UserNamesRequirement();
  </code></pre>
  
  <p>For <code>.AddSingleTon</code> in startup file I get below error:</p>
  
  <p><a href="https://i.stack.imgur.com/FcZZO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FcZZO.png" alt="enter image description here"></a></p>
  
  <p>Here is the <code>handler</code> class:</p>
  
  <pre><code>public class UserNamesHandler : AuthorizationHandler&lt;UserNamesRequirement&gt;
  {
  
      protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNamesRequirement requirement)
      {
          var userName = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
  
          if (requirement.Users.ToList().Contains(userName))
              context.Succeed(requirement);
          return Task.FromResult(0);
      }
  }
  </code></pre>
  
  <p>Here is is the UserNamesRequirement class:</p>
  
  <pre><code>public class UserNamesRequirement : IAuthorizationRequirement
  {
      public UserNamesRequirement(params string[] UserNames)
      {
          Users = UserNames;
      }
      public string[] Users { get; set; }
  }
  </code></pre>
  
  <p><strong>UPDATE 3:</strong> SOLVED!!!!</p>
  
  <p>Here are few changes that were added from <strong>update 2</strong>:</p>
  
  <p>In <code>UserNameshandler</code> class changed var userName to get values from <code>context.User.Identity.Name;</code></p>
  
  <pre><code>public class UserNamesHandler : AuthorizationHandler&lt;UserNamesRequirement&gt;
  {
  
      protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNamesRequirement requirement)
      {
          // var userName = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
          var userName = context.User.Identity.Name;
  
          if (requirement.Users.ToList().Contains(userName))
              context.Succeed(requirement);
          return Task.FromResult(0);
      }
  }
  </code></pre>
  
  <p>In StartUp class fixed from <code>services.AddSingleton&lt;IAuthorizationHandler, UserNamesRequirement&gt;();</code> to <code>services.AddSingleton&lt;IAuthorizationHandler,UserNamesHandler&gt;();</code> </p>
  
  <p>Thanks to Gevory. :)</p>