https://github.com/dark-loop/webjobs-authorize
Bringing AuthorizeAttribute Behavior to Azure Functions
https://github.com/dark-loop/webjobs-authorize
Last synced: 9 days ago
JSON representation
Bringing AuthorizeAttribute Behavior to Azure Functions
- Host: GitHub
- URL: https://github.com/dark-loop/webjobs-authorize
- Owner: dark-loop
- License: apache-2.0
- Created: 2019-03-26T19:33:00.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-08T14:54:34.000Z (almost 5 years ago)
- Last Synced: 2025-08-01T07:24:40.146Z (6 months ago)
- Language: C#
- Size: 37.1 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webjobs-authorize
Bringing AuthorizeAttribute Behavior to Azure Functions v2. For v3 compatibility use [functions-authorize](https://github.com/dark-loop/functions-authorize).
It hooks into .NET Core dependency injection container to enable authentication and authorization in the same way ASP.NET Core does.
## License
This projects is open source and may be redistributed under the terms of the [Apache 2.0](http://opensource.org/licenses/Apache-2.0) license.
## Using the package
### Installing the package
`dotnet add package DarkLoop.Azure.WebJobs.Authorize`
### Setting up authentication
The goal is to utilize the same authentication framework provided for ASP.NET Core
```c#
using Microsoft.Azure.WebJobs.Hosting;
using MyFunctionAppNamespace;
[assembly: WebJobsStartup(typeof(Startup))]
namespace MyFunctionAppNamespace
{
class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder
.AddAuthentication(options =>
{
options.DefaultAuthenticationScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
options.ClientId = "";
// ... more options here
})
.AddJwtBearer(options =>
{
options.Audience = "";
// ... more options here
});
builder
.AddAuthorization(options =>
{
options.AddPolicy("OnlyAdmins", policyBuilder =>
{
// configure my policy requirements
});
});
}
}
}
```
No need to register the middleware the way we do for ASP.NET Core applications.
### Using the attribute
And now lets use `WebJobAuthorizeAttribute` the same way we use `AuthorizeAttribute` in our ASP.NET Core applications.
```C#
public class Functions
{
[WebJobAuthorize]
[FunctionName("get-record")]
public async Task GetRecord(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
ILogger log)
{
var user = req.HttpContext.User;
var record = GetUserData(user.Identity.Name);
return new OkObjectResult(record);
}
[WebJobAuthorize(Policy = "OnlyAdmins")]
[FunctionName("get-all-records")]
public async Task(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
ILogger log)
{
var records = GetAllData();
return new OkObjectResult(records);
}
}
```
##
### Releases
[](https://www.nuget.org/packages/DarkLoop.Azure.WebJobs.Authorize)
### Builds
