An extendible framework for .NET to implement the BFF Security Pattern (a.k.a. Token Handler Pattern) in Single Page Applications.
$ dotnet add package OidcProxy.Net.Auth0This package contains the software you need to implement the BFF Security Pattern. This software does three things:
OidcProxy.Net is a stateful reverse proxy. To forward requests to downstream services OidcProxy.Net uses YARP.
Currently, OidcProxy.Net supports logging in with Azure, Auth0, IdentityServer4, and any other OpenID Connect compliant authorization server. Currently, only the Authorization Code flow with Proof-Key Client Exchange is supported.
To build it, execute the following commands:
dotnet new web
dotnet add package OidcProxy.Net.Auth0
Create the following Program.cs file:
using OidcProxy.Net.Auth0;
using OidcProxy.Net.ModuleInitializers;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration
.GetSection("OidcProxy")
.Get<Auth0ProxyConfig>();
builder.Services.AddAuth0Proxy(config);
var app = builder.Build();
app.UseAuth0Proxy();
app.Run();
Create the following appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"OidcProxy": {
"LandingPage": "/hello",
"Auth0": {
"ClientId": "{yourClientId}",
"ClientSecret": "{yourClientSecret}",
"Domain": "{yourDomain}",
"Audience": "{yourAudience}",
"Scopes": [
"openid",
"profile",
"email"
]
},
"ReverseProxy": {
"Routes": {
"api": {
"ClusterId": "api",
"Match": {
"Path": "/api/{*any}"
}
}
},
"Clusters": {
"api": {
"Destinations": {
"api/node1": {
"Address": "https://{your_api}/"
}
}
}
}
}
}
}
In this example we assume you are running a Single Page Application on localhost on port 4200 and you have an API running at localhost on port 8080. If that is not the case, then update the appsettings.json accordingly.
To run it, type dotnet run or just hit the 'play'-button in Visual Studio.
The proxy relays all requests as configured in the ReverseProxy section in the appsettings.json file, except for four endpoints:
To log a user in and to start a http session, navigate to /.auth/login. The software will redirect to the login page of the Identity Provider to log the user in. The resulting tokens will be stored in the user session and are not available in the browser.
This endpoint is used by the IdentityProvider.
To see the logged in user, navigate to the /.auth/me endpoint. This endpoint shows the claims that are in the id_token.
To revoke the tokens that have been obtained when the user logged in, execute a get request on the /.auth/end-session endpoint. This will revoke the tokens that have been stored in the user session and will not log the user out from the Identity Provider session. This must be implemented at client side.
Are you encountering issues? Please let us know at: https://github.com/thecloudnativewebapp/OidcProxy.Net/issues