I have MiniProfiler set up in an ASP.NET Core application. Profiling works fine.
However, I only want Admins to be able to profile.
I have the following in ConfigureServices:
services.AddMiniProfiler(options =>
{
options.ShouldProfile = request =>
request.HttpContext.User.IsInRole("Admin");
});
The problem is, the user identity does not seem to be loaded in that method.
The User.Identity.Name property is null, and there are no claims.
My guess is that this call happens before that info is populated?
How can I profile based on the user identity?
You need to know that according to the docs the
ClaimsPrincipal.IsInRole()method checks for Claims of typeClaimsIdentity.RoleClaimType.Be sure you have added the role claims.Here is a working demo you could follow:
1.Register the user with name
[email protected]successfully.2.Generate the role and add the role with claims to the user:
2.Startup.cs:
Result: