asp.net core w/ cookie middleware - accessing request data on authorization

steamrolla

This question is essentially the same as the one here, but, for asp.net core while using the asp.net core cookie middleware.

Is accessing query string/request body data possible on validation, and if it is, would you encourage the idea? It seems that according to this that it is very much possible, however, are the same rules in play from big boy asp.net (such as you are only to read the request data once in a given requests lifetime)?

Example: I'm creating an app where people have one account, but, are members of different teams. They can perform many different actions in the app, and, they can perform that action while in the "context" of one team or another that they are a member of. So, I have a teamId integer being passed in requests made to the server. I'd like to pull claims off the ClaimsPrincipal verifying that they really are a member of that team in the authorization portion of the pipeline.

adem caglin

As you said it is possible to access request's data on OnValidatePrincipal event. So, you can write something like this:

OnValidatePrincipal = async (context) =>
{
      if (context.Request.Path.Value.StartsWith("/teams/")) 
      {
          var teamId = // get team id from Path;

          if (user is not team member)
          {
              context.Response.StatusCode = 403;
          }
      }
} 

However, i think your requirement is related Authorization rather than Authentication. I would use Policy-Based Authorization to handle the requirement. Example policy should be like this:

Requirement and Handler:

public class TeamMemberHandler: AuthorizationHandler<TeamMemberRequirement>
{
    private readonly IActionContextAccessor _accessor; // for getting teamId from RouteData
    public TeamMemberHandler(IActionContextAccessor accessor)
    {
        _accessor = accessor;
    }
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TeamMemberRequirement requirement)
    {
        var teamId = // get teamId with using _accessor
        if (user is not member of team(by teamId))
        {
            context.Fail();
        }
        return Task.FromResult(0);
    }
}
public class TeamMemberRequirement : IAuthorizationRequirement
{
}

Configure Services:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    services.AddAuthorization(options =>
    {
        options.AddPolicy("TeamMember",
                          policy => policy.Requirements.Add(new TeamMemberRequirement()));
    });

    services.AddSingleton<IAuthorizationHandler, TeamMemberHandler>();
}

Finally use it on top of controller(or if you want, you can add filter globally)

Authorize[(Policy = "TeamMember")]
public class TeamHomeController : Controller
{
    // Authorize[(Policy = "AnotherPolicy")]
    public IActionResult Index(){}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET core Authorization Middleware

Authorization middleware exception in ASP.NET Core with Angular project

Relation between Authorization middleware and filter Asp.net core

Asp.net core 2.0 middleware - accessing config settings

Asp.net core middleware routing delay between request and action

Replacing Request/Response Body in asp.net core middleware

.net core 3 middleware or authorization attribute ? and how to?

.Net Core Middleware - Getting Form Data from Request

How to pass primitive data to asp.net core middleware

ASP.NET Core middleware or OWIN middleware?

Cookie Middleware without Identity ASP.NET Core v2

ASP .NET Core FormsAuthentication Cookie

Which authorization policy is used if multiple policies are specified (e.g. middleware configuration, controller/action attribute,) in Asp.Net Core?

Request (using authorization in header) to asp.net core API from Angular 4 not working

Authorization Bearer token not being sent in request using Swagger in Asp.Net Core

Is it possible to redirect request from middleware in .net core

how to write Middleware to measure request processing time in asp.net core 2.0

Is there any way within middleware running on ASP.NET Core 2.2 to detect if the request is for an ApiController?

How do I get the incoming request body and the outgoing response body in ASP.NET Core middleware?

How to request authentication from custom middleware in ASP.NET Core 2.0

Asp.Net Core 2 oidc middleware does not challenge after proxy request returns 401

How to read request body multiple times in asp net core 2.2 middleware?

.NET Core 7, using a different assembly for authorization middleware

Asp Net Core 3.1 Authorization by Custom Roles

ASP.Net Core 5.0 Authentication and Authorization

Asp net core MVC Authorization with Active Directory

ASP.NET Core 5.0 Customizing Authorization

Claim based authorization in ASP.NET Core

ASP.NET Core with optional authentication/authorization