Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 441 forks source link

Thread.CurrentPrincipal not being populated #4860

Closed pizerg closed 5 years ago

pizerg commented 5 years ago

Trying to enable Google auth for functions v2 using a provider SDK but Thread.CurrentPrincipal is never populated.

Investigative information

Please provide the following:

Repro steps

1) Configure the functions project to enable authentication, configure Google credentials (key + secret) and disable tokens store (although if tokens store is enabled will get same results). Configure WEBSITE_AUTH_HIDE_DEPRECATED_SID parameter to "true". Enable "Action to take when request is not authenticated" option with "Allow Anonymous Requests (no action)" value.

2) Authenticate on mobile using Google's SDK and then send the token to FUNCTIONS_HOST/.auth/google which returns azure's authentication token.

3) Perform a call to a different function which requieres authentication, including azure's authentication token from the previous step in X-ZUMO-AUTH header.

4) Retrieve user information through Thread.CurrentPrincipal in the function's body.

Expected behavior

Thread.CurrentPrincipal is populated with the authenticated user information

Actual behavior

Thread.CurrentPrincipal is always empty

Related information

The function's definition is the following:

[FunctionName("ExternalProviderLogin")]
public static async Task<IActionResult> ExternalProviderLogin([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req, ILogger log)
brettsam commented 5 years ago

@ConnorMcMahon -- are you the expert here?

pizerg commented 5 years ago

If it helps with the issue, I've been doing further testing and it appears that if I inject ClaimsPrincipal principal as a parameter in the function, then I'm able to retrieve user data from it (but it still fails to get the information from Thread.CurrentPrincipal)

ConnorMcMahon commented 5 years ago

@pizerg, this is intentional. .NET Core made the decision to not assign Thread.CurrentPrincipal. Since Functions V2 is .NET Core, we decided not to set it ourselves as that goes against the design principals of the framework.

Instead, we did the work that you have already discovered that allows you to populate the ClaimsPrincipal object as a parameter. You can read more about this feature here.

Hope that clarifies matters.

pizerg commented 5 years ago

Thanks for the clarification, it's now working again!