BenMorris / FunctionsCustomSercuity

A basic sample demonstrating how custom input binding can be used to support custom authentication for Azure Functions.
76 stars 22 forks source link

AccessTokenValueProvider.GetValueAsync incorrect return #4

Closed tbusot closed 5 years ago

tbusot commented 5 years ago

The function signature expects a ClaimsPrincipal.

The method GetValueAsync is an async function and the return type is therefore automatically wrapped in a Task.

As such, rather than returning like this:

return Task.FromResult<ClaimsPrincipal>(result);

which will throw the following cast exception:

System.Private.CoreLib: Exception while executing function: GetInvitation. Anonymously Hosted DynamicMethods Assembly: Unable to cast object of type 'System.Threading.Tasks.Task1[System.Security.Claims.ClaimsPrincipal]' to type 'System.Security.Claims.ClaimsPrincipal'.

You should return the result directly:

return result;

BenMorris commented 5 years ago

The result is not passed directly. If you run the sample the result gets picked up by the Azure Functions runtime and is passed to the function. It runs just fine.

The GetValueAsync method is not marked as async as there are no blocking calls. I guess there is an argument for making the method asynchronous as this is implied by the interface - i.e. "GetValueAsync". In which case the code would look more like this:

public async Task<object> GetValueAsync()
{
  // [...validate the token...]
  return await Task.FromResult<object>(result);
}
HARIKSREEE commented 4 years ago

Thanks a lot, This helped us