title: 500 Internal Server Error with Policy based authentication asp.net core
body: <p>I, am trying to do policy based authentication in asp.net core 2.1. Due to dependency injection I, am facing 500 internal server error. Not able to figure it out how to solve the issue.</p>
  
  <pre><code>services.AddAuthorization(auth =&gt;
              {
                  auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                      .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                      .RequireAuthenticatedUser().Build());
  
                  auth.AddPolicy("canRead",
                                 policy =&gt; policy.Requirements.Add(new PermissionRequirement(PermissionEnum.Read)));
                  auth.AddPolicy("canCreate",
                                 policy =&gt; policy.Requirements.Add(new PermissionRequirement(PermissionEnum.Write)));
                  auth.AddPolicy("canUpdate",
                                 policy =&gt; policy.Requirements.Add(new PermissionRequirement(PermissionEnum.Update)));
                  auth.AddPolicy("canDelete",
                                 policy =&gt; policy.Requirements.Add(new PermissionRequirement(PermissionEnum.Delete)));
              });
              services.AddSingleton&lt;IAuthorizationHandler, PermissionHandler&gt;();
  </code></pre>
  
  <p>Here is the permission check code</p>
  
  <pre><code>public class PermissionRequirement : IAuthorizationRequirement
      {
          public PermissionEnum _permission { get; }
          public PermissionRequirement(PermissionEnum permission)
          {
              _permission = permission;
          }
      }
  
      public class PermissionHandler : AuthorizationHandler&lt;PermissionRequirement&gt;
      {
  
          private readonly IAdminUOW permissionRepository;
  
          public PermissionHandler(IAdminUOW permissionRepository)
          {
              this.permissionRepository = permissionRepository;
          }
  
          protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
          {
              if (context.User == null)
              {
                  // no user authorizedd. Alternatively call context.Fail() to ensure a failure 
                  // as another handler for this requirement may succeed
                  context.Fail();
              }
              bool hasPermission = await permissionRepository.CheckPermissionForUser(new Guid(context.User.ToString()), requirement._permission);
              if (hasPermission)
              {
                  context.Succeed(requirement);
              }
  
          }
  </code></pre>
  
  <p><strong>Permission Enum</strong></p>
  
  <pre><code>[Flags]
      public enum PermissionEnum
      {
          None = 0,
          Read = 0x001,
          Write = 0x010,
          Delete = 0x011,
          Update = 0x100,
          ReadWrite = Read | Write,
          FullControl = ReadWrite | Update | Delete
      }
  }
  </code></pre>
  
  <p>The error is causing by <code>services.AddSingleton&lt;IAuthorizationHandler, PermissionHandler&gt;();</code> But don't know what wrong I, am doing.</p>
  
  <p>Can anyone please let me know how to resolve this issue.</p>
  
  <p>References - <a href="https://stormpath.com/blog/tutorial-policy-based-authorization-asp-net-core" rel="nofollow noreferrer">https://stormpath.com/blog/tutorial-policy-based-authorization-asp-net-core</a></p>