![]() |
VOOZH | about |
dotnet add package Formula.SimpleResourceServer --version 1.8.0
NuGet\Install-Package Formula.SimpleResourceServer -Version 1.8.0
<PackageReference Include="Formula.SimpleResourceServer" Version="1.8.0" />
<PackageVersion Include="Formula.SimpleResourceServer" Version="1.8.0" />Directory.Packages.props
<PackageReference Include="Formula.SimpleResourceServer" />Project file
paket add Formula.SimpleResourceServer --version 1.8.0
#r "nuget: Formula.SimpleResourceServer, 1.8.0"
#:package Formula.SimpleResourceServer@1.8.0
#addin nuget:?package=Formula.SimpleResourceServer&version=1.8.0Install as a Cake Addin
#tool nuget:?package=Formula.SimpleResourceServer&version=1.8.0Install as a Cake Tool
A simple OAuth2 / OpenID Connect Resource Server wrapper for Identity Server.
By default, Simple Resource Server validates authentication against an authority using JWT ( see here for more on jwt ) passed using using a Bearer token in the Authorization header of the request made from the client.
To enable a project to serve as a resource server against an authority you will need to prepare configuration and inject it correctly in Startup.cs.
Add the following using;
using Formula.SimpleResourceServer;
Some extension methods have been provided for you register your configuration. Within the ConfigureServices function of Startup.cs you can call services.AddSimpleResourceServer providing it with an implementation of ISimpleResourceServerConfig.
This can be done by creating your own class that implements the ISimpleResourceServerConfig contract, manually, however a more common way to provide configuration is via a JSON configuration file within the project using the ResourceServerConfigLoader.
(See ResourceServerConfigDefinition for configuration options)
services.AddSimpleResourceServer(ResourceServerConfigLoader.Get("resourceServerConfig.json"));
You may also provide some defaults using a delegate.
services.AddSimpleResourceServer(ResourceServerConfigLoader.Get("resourceServerConfig.json", () =>
{
var def = new ResourceServerConfigDefinition();
def.Authority = "http://localhost:5000";
def.RequireHttpsMetadata = false;
def.Audience = "my-api";
return def;
}));
(See ConfigLoader in Formula.SimpleCore for details on how this functionality may be leverage for other task)
If you handle other additional authentication mechanisms, you may pass your AuthenticationBuilder as a second parameter to this function, otherwise it is assumed that no other calls to AddAuthentication have been configured.
In the configure section of your app, you may call;
app.UseSimpleResourceServer();
This should be done before other calls to UseAuthorization.
A service is provided, allowing you to extract certain claims about the user making the request. This class IdentityDetails can provide the user id, and other details (providing you have configured your authorization server to provide these claims). This was designed with Open ID Connect / OAuth 2 and was tested using IdentityServer 4. Obtaining the user id available without any additional configuration, however some properties require additional claims be set up.
You can retrieve the identity details as follows.
var details = new Formula.SimpleResourceServer.IdentityDetails(httpContextAccessor);
var userId = details.UserId;
// The following properties require additional configuration on the authorization server
var email = details.Email;
var roles = details.Roles;
var isAdmin = details.HasRole("Administrator");
In order to use "Role Based Authorization", the "scope" for your resource, needs to include additional claims. At a minimum role, however it is suggested to enable the following useful claims (role, email, name).
This configuration will be mentioned from the perspective of IdentityServer 4. For your resource (see ApiResources table), you need to set up API Claims for; role, email and name (see the ApiClaims table).
References used for this were;
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.