简体   繁体   中英

Azure functions authentication with ASP.NET Identity and JWT token

I am taking on a new development stack where I am planning to go serverless using the Azure functions .

I am thinking that my application should use ASP.NET Identity to store user credentials and other details. and he should get authenticated across Identity DB using JWT tokens .

I am trying to get hold of some examples that might be helpful to see how to implement ASP.NET Identity in Azure functions. Can anyone point me to the right direction?

i don't think there is a efficient way to do this. because azure functions don't really have a startup or entry point in the entire app where you can plug in oauth , which is usually done in the mvc or web apps in startup.cs or global.asax.

So this has to divert a little bit from how it's done in a web application

maybe this can be of some help

https://vincentlauzon.com/2017/12/04/azure-functions-http-authorization-levels/

I've had to do something like this recently, ie authenticate against JWT access tokens issued by a third party OAuth Server. There's no convenient middleware that lets you do this, so you're stuck with a manual implementation.

The approach I used was to create a custom input binding that lets you validate the token and inject a ClaimsPrincipal into the function definition. The resulting function definition looks something like this:

[FunctionName("ExampleHttpFunction")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "example")] HttpRequest req, 
    ILogger log, 
    [AccessToken] ClaimsPrincipal principal)
{
    log.LogInformation($"Request received for {principal.Identity.Name}.");
    return new OkResult();
}

This does at least separate authentication from the function definition and make things a little more testable.

You need to wire together half a dozen different classes to make it work, but I have written up the detail here: custom token authentication for Azure Functions . There is also some example code on GitHub that you can look at.

Hope this helps...!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM