Skip to content

access HttpContext through HttpContextAccessor in ASP.NET Core 3.0 #12564

Closed
@PudottaPommin

Description

@PudottaPommin

Hello,

I found problem when I converted app from .NET Core 2.2 into 3.0.
In 3.0 is context.Resource not anymore of type AuthorizationFilterContext, but instead Microsoft.AspNetCore.Routing.RouteEndpoint. Is there anything I'm missing (some changes in configuration or some other place)?
If did really changed, is there any way to redirect from handlers?
Any help is greatly appreciated.

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                      T requirement)
{
    var resource = context.Resource;     // 2.2 - AuthorizationFilterContext
                                         // 3.0 - RouteEndpoint

    if (context.Resource is AuthorizationFilterContext mvcContext) // in 3.0 results in null
    {
    }
}

Thank you


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

added this to the Backlog milestone on May 28, 2019
FeodorFitsner

FeodorFitsner commented on Jun 11, 2019

@FeodorFitsner

This is the solution: dotnet/aspnetcore#11075

PudottaPommin

PudottaPommin commented on Jul 19, 2019

@PudottaPommin
Author

Not exactly, because I need to set RedirectToActionResult to redirect from this handler into specified action. Like

if (context.Resource is AuthorizationFilterContext mvcContext)
{
    mvcContext.Result = new RedirectToActionResult("Action", "Controller", null);
}            
sbwalker

sbwalker commented on Aug 27, 2019

@sbwalker

context.Resource used to provide access to HttpContext so that you could enforce restrictions based on various aspects of the Request, etc... for example: https://www.khalidabuhakmeh.com/asp-net-core-resource-authorization-with-authorizationhandler. How can this be done in ASP.NET Core 3?

PudottaPommin

PudottaPommin commented on Sep 14, 2019

@PudottaPommin
Author

@sbwalker your link is broken

Rick-Anderson

Rick-Anderson commented on Sep 14, 2019

@Rick-Anderson
Contributor

@HaoK should this go in the 2.2 to 3.0 migration guide?

sbwalker

sbwalker commented on Sep 14, 2019

@sbwalker

Sorry.. the link was working when I posted it. I found the answer to my question - you now need to access HttpContext through HttpContextAccessor:

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
    {
        var ctx = HttpContextAccessor.HttpContext;
        if (ctx != null && ctx.Request.Query.ContainsKey("entityid"))
        {
            int EntityId = int.Parse(ctx.Request.Query["entityid"]);
            if (UserPermissions.IsAuthorized(context.User, requirement.EntityName, EntityId, requirement.PermissionName))
            {
                context.Succeed(requirement);
            }
        }
        return Task.CompletedTask;
    }
HaoK

HaoK commented on Sep 24, 2019

@HaoK
Member

This is a sideeffect of the endpoint routing changes, so maybe it should go with whatever docs are related to that change?

changed the title [-].NET Core 3.0 accessing MVC request context[/-] [+]access HttpContext through HttpContextAccessor in ASP.NET Core 3.0[/+] on Sep 24, 2019

26 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @analogrelay@kierenj@tdykstra@PudottaPommin@Rick-Anderson

      Issue actions

        access HttpContext through HttpContextAccessor in ASP.NET Core 3.0 · Issue #12564 · dotnet/AspNetCore.Docs