Integrate MiniProfiler with .NetCore 3.1

1.3k Views Asked by At

I want to integrate MiniProfiler is a WebApi or View /XX/results-index. The WebApi is authenticated with Bearer Tokens. I only want Group Users in Active Directory can see the results, but I don't get it.

I have this code in ServicesCollection:

services.AddMiniProfiler(options =>
{
   options.RouteBasePath = "/profiler";
   options.ResultsAuthorizeAsync = async request => await GetAuthorization(request);            }).AddEntityFramework();

private static async Task<bool> GetAuthorization(HttpRequest request)
{
 //   var user = request.HttpContext.User.Identity.Name; --> Is null
   return true;
}

In Configure Method in StartUp:

app.UseSwagger().UseSwaggerUI(options =>
{
   options.SwaggerEndpoint($"/swagger/v1/swagger.json", $"{env.ApplicationName} V1");
   options.OAuthClientId("TestApiswaggerui");
   options.OAuthAppName("TestApi Swagger UI");
   options.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream(
                        "TestApi.SwaggerMiniProfiler.html");
})
.UseMiniProfiler();

I want to see mini profiler information through some options:

  • http://localhost:5050/profiler/results-index --> Show the list methods called
  • http://localhost:5050/swagger/index.html --> Show the MiniProfiler in the same page

Environment:

.NET Core version: 3.1
MiniProfiler version: MiniProfiler.AspNetCore.Mvc v.4.2.1
Operative system: Windows 10
2

There are 2 best solutions below

4
Nick Craver On

The piece you're probably missing here is that MiniProfiler shows your results. What's "you" is determined by the UserIdProvider option. When recording and viewing profiles, ensure that these are the same "user ID" (defaults to IP address). It looks like this in options:

services.AddMiniProfiler(options =>
{
   options.UserIdProvider = request => ConsistentUserId(request);
});

If your swagger has zero server-side processing at all (e.g. it does not include the MiniProfiler <script> tag from .RenderInludes() or the <mini-profiler /> tag helper, then the issue isn't viewing the profiles so much as not even attempting to view. There are some ideas I have around a static tag without profiles to currently view, but I do not know how to get them into Swagger in it's generation phase (just not familiar enough). Note that it's a blatant hack, but you could work around the issue at the moment with a manual script tag. You'll want to follow https://github.com/MiniProfiler/dotnet/issues/326 for this.

0
Nacho On

I just want to leave the option of having the traces read for that group from the active directory:

 services.AddMiniProfiler(options =>
                    {
                        // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
                        options.RouteBasePath = "/profiler";

                        options.ColorScheme = StackExchange.Profiling.ColorScheme.Light;
                        options.PopupRenderPosition = StackExchange.Profiling.RenderPosition.BottomLeft;
                        options.PopupShowTimeWithChildren = true;
                        options.PopupShowTrivial = true;
                        options.ShouldProfile = ShowProfile;
                        options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();

                        options.ResultsAuthorize = request => request.HttpContext.User.IsInRole("S-INFORMATICA");
 })
                    .AddEntityFramework();