Skip to content

Set ClaimsPrincipal in Custom Binding #4851

@vitalybibikov

Description

@vitalybibikov

Hello,

How is it possible not to set ClaimsPrincipal for Azure Functions v2?

When used locally the AF host sets ClaimsPrincipal with default values, as described in #33

What I want to achieve is to set ClaimsPrincipal with my own values, that are parsed out of the token inside of Custom Binding:

  public class Example
    {
        [FunctionName("Example")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestMessage req,
            [FunctionToken("Manager", "Worker", "Owner")] FunctionTokenResult token,
            ClaimsPrincipal principal,
            ILogger log)
        {
            var identity = token.Principal.Claims.First(x => x.Type == ClaimTypes.NameIdentifier);
            return await Handler.WrapAsync(token,async () =>
            {
                log.LogInformation("C# HTTP trigger function processed a request.");
                return new OkObjectResult($"Hello, {token}");
            });
        }

    }

Full example: here

So, in the example above I have:

  1. FunctionTokenResult token which contains Principal property that was set by me.
  2. ClaimsPrincipal principal that is set by the environment and contains default instance for the host.

I'm trying to substitute that default ClaimsPrincipal somewhere inside of the custom binding and be able to inject my own ClaimsPrincipal instance across the app.

Though I'm able to set it like so:

Request.HttpContext.User = claimsPrincipal;

I believe it should be possible to substitute ClaimsPrincipal with the desired one for the request.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions