Performance implications of using MapWhen?

23 Views Asked by At

Please see the code below. I'm concerned about the potential performance implications of this code, as I realised that MapWhen executes on every request to the server - including requests for images and other non-page-related content.

The actual code doesn't have much "weight" - executing MyFunc will take an insignificant amount of time. However, does the mere fact that it needs to execute the function in and of itself cause a performance hit, if it is happening on every request for content?

If so, is there a better way of doing what I'm trying to do? Should I be using IAppBuilder.Use instead?

public void Configuration(IAppBuilder app)
{
    app.MapWhen(ctx => MyFunc(ctx), map =>
    {
        // do stuff
    });
}

private bool MyFunc(IOwinContext ctx)
{
    bool isAuthenticated = ctx.Authentication.User.Identity.IsAuthenticated;
    bool isMyPath = ctx.Request.Path.Value.ToLower().StartsWith("/MyPath");
    if (!isMyPath)
        return false;
    else if (!isAuthenticated || !(ctx.Authentication.User.Identity is ClaimsIdentity claimsIdentity))
        return true;
    else
        return HasMyClaim(claimsIdentity);
}

private bool HasMyClaim(ClaimsIdentity claimsIdentity)
{
    return !claimsIdentity?.Claims?.Any(claim =>
        claim.Type == "MyClaimType" && claim.Value == "MyClaimValue") ?? false;
}
0

There are 0 best solutions below