A simple yet scalable OAuth 2.0 provider for the 4 standard types of OAuth 2.0 and custom grants.
10K+
OAuthServer.NET is a simple yet scalable OAuth 2.0 provider for the 4 standard types of OAuth 2.0 (Implicit, Authorization Code, Resource Owner Password, and Client Credentials), proof key for code exchange (PKSE) for native Authorization Code clients, and custom grants. OAuthServer.NET supports local login and 3rd party logins including Google, Facebook, Twitter, Microsoft, and Twitch. OAuthServer.NET is integrated with the Asp.NET Identity system and includes full managemant of accounts including password management and multiple 3rd party logins per account. OAuthServer.NET includes a UI (created in Angular CLI) to easily set up and configure clients. OAuthServer.NET is easily deployed through Docker, NuGet, or can be modified directly from the source https://github.com/liveordevtrying/oauthserver.net.
There are 3 ways to implement OAuthServer.NET:
OAuthServer.NET includes 2 projects - 1 for the OAuthServer itself and another (OAuthServer.NET - UI) for configuring the OAuthServer.
The quickest way to get started with OAuthServer.NET and OAuthServer.NET - UI is to use the official OAuthServer.NET Docker images.
You can spin up an unconfigured OAuthServer.NET using Docker. You must set the required environmental variables detailed below. An example of a docker run command for OAuthServer.NET is:
docker run -p 80:80 -d -e ConnectionStrings__Database=Server=localhost;Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=;MultipleActiveResultSets=true -e Admin__Password=Password1! liveordevtrying/oauthserver.net:latest
This command can be use with docker-compose as follows:
oauthserver.net:
image: liveordevtrying/oauthserver.net:latest
hostname: oauthserver.net
ports:
- "80:80"
restart: always
environment:
- ConnectionStrings__Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=;MultipleActiveResultSets=true
- Admin__Password=Password1!
When OAuthServer.NET starts for the first time, the database will be automatically created for the database specified in the connection string. The following tags are available for OAuthServer.NET on DockerHub:
- liveordevtrying/oauthserver.net:latest
- liveordevtrying/oauthserver.net:arm32v7
- liveordevtrying/oauthserver.net:ui
- liveordevtrying/oauthserver.net:ui-arm32v7
If additional tags / platforms are required, please post them as an issue at [the public git repository] for this project or send an email to [email protected] and new tags will be added promptly.
Example: Server=localhost;Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=Password1!;MultipleActiveResultSets=trueExample: Password1!admin.IEmailSender and code in logic to send emails. The IEmailSender and implemented class must be registered in Startup.cs ConfigureServices for OAuthServer's dependency injection to find the implemented email sender - otherwise after login, user's will be sent to an error page after login specifying that no implementation for IEmailSender could be located.Before installing OAuthServer.NET - UI, make sure that OAuthServer.NET has successfully started at least one time and the database was created. OAuthServer.NET UI is designed to interact with OAutherServer's existing database - starting OAuthServer.NET UI will not create a database.
To use the OAuthServer.NET - UI Docker image, you must set the required environmental variables detailed below and set the connection string to the same connection string used for OAuthServer.NET. An example of a docker run command for OAuthServer.NET - UI is:
docker run -p 80:80 -d -e ConnectionStrings__Database=Server=localhost;Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=Password1!;MultipleActiveResultSets=true liveordevtrying/oauthserver.net:ui
This command can be use with docker-compose as follows:
oauthserver.net:
image: liveordevtrying/oauthserver.net:ui
hostname: oauthserver.net
ports:
- "80:80"
restart: always
environment:
- ConnectionStrings__Database=Server=localhost;OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=Password1!;MultipleActiveResultSets=true
Example: Server=localhost;Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=Password1!;MultipleActiveResultSets=trueOAuthServer.NET is available on NuGet and easily implemented with extension methods for methods in Startup.cs. OAuthServer.NET - UI is not available on NuGet but can be implemented with Docker (above) or with Source Code (below).
To install OAuthServer.NET through NuGet, first create a .NET 5 web application without authentication (Figure 1).
0pen the NuGet Package Manager in Visual Studio and issue the following command:
install-package oauthserver.net
This will install OAuthServer.NET and dependencies in the new project. Once completed, we need to use the provided extension methods to enable required services and configuration settings.
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.StartOAuthServer(new OAuthServerParams
{
AdminPassword = Configuration["Admin:Password"],
AdminUsername = Configuration["Admin:Username"],
OAuthCookieExpirationDays = int.Parse(Configuration["OAuthServerCookieExpirationDays"]),
ConnectionString = Configuration["ConnectionStrings:Database"],
RequireConfirmedEmail = bool.Parse(Configuration["RequireConfirmedEmail"]),
Show3rdPartyLoginGraphics = bool.Parse(Configuration["Show3rdPartyLoginGraphics"]),
ExternalProviders = new OAuthServerExternalProviders
{
GoogleClientId = Configuration["Google:ClientId"],
GoogleClientSecret = Configuration["Google:ClientSecret"],
TwitchClientId = Configuration["Twitch:ClientId"],
TwitchClientSecret = Configuration["Twitch:ClientSecret"],
FacebookClientId = Configuration["Facebook:ClientId"],
FacebookClientSecret = Configuration["Facebook:ClientSecret"],
TwitterClientId = Configuration["Twitter:ClientId"],
TwitterClientSecret = Configuration["Twitter:ClientSecret"],
MicrosoftClientId = Configuration["Microsoft:ClientId"],
MicrosoftClientSecret = Configuration["Microsoft:ClientSecret"],
}
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, OAuthServerServices services)
{
app.StartOAuthServer(services);
}
Example: Server=localhost;Database=OAuthServer.NET;Trusted_Connection=False;User Id=sa;Password=Password1!;MultipleActiveResultSets=trueExample: Password1!admin.IEmailSender and code in logic to send emails. The IEmailSender and implemented class must be registered in Startup.cs ConfigureServices for OAuthServer's dependency injection to find the implemented email sender - otherwise after login, user's will be sent to an error page after login specifying that no implementation for IEmailSender could be located.Unfortunately, it is not feasible for OAuthServer.NET-UI to be available on NuGet: to spin up the UI, please refer to the above Docker implementation or the SourceCode implementation below.
OAuthServer.NET is completely open-source and highly scalable using the patterns found within the sourcecode. Using sourcode, you can easily create newor update existing grant flows and authorization flows.
First, go to GitHub.com and download / clone the project and extract / pull into a directory of your choosing.
Once you have the sourcecode, you need to create a new .NET 5 (or other specified supported version) web application and add the pulled class library as a reference to the new web application. Following the steps above for NuGet, you need to modify the Startup.cs to use the provided extension methods.
At a high level, the project is structured with 3 controllers - Authorize, Login, and Token. Each controller has its own business logic layer (BLL) containing logic specific for the controller's actions. Each BLL has a reference to a data-access-layer (DAL) which by default is Entity Framework with SQL server.
When an authorize request is received, the applicaiton will look for the approriate IAuthorizeTypeProvider that matches the provided grant_type. The grant_type will need to be registered in the OAuthServer.NET - UI prior to being implemented in code. To add a new grant type, create a new class and implement the interface IAuthorizeTypeProvider. The required function, ValidateAsync(HttpRequest request), will contain the logic to follow for the specific grant. 2 authorize type providers are provided by default - AuthorizeTypeProviderAuthorizationCode and AuthorizeTypeProviderImplicit. ValidateAsync() returns a compound result of (bool, string), where the bool is the flag to indicate is validation is successful, and the string represents the error message if the authorization was not successful.
To create a new Authorization Grant, first log into the OAuthServer.NET - UI, navigate to Grants, and create the new Grant with the requested AuthorizeResponseType. The 2 default registered response types are code and token - make sure to choose an AuthorizeResponseType not already in use.
Next, create a new class in your web application and implement IAuthorizeTypeProvider. You will be required to specify a AuthorizeResponseType (which should exactly match the AuthorizeResponeType specified in the OAuthServer.NET - UI Grant created above), and ValidateAsync(HttpRequest request). ValidateAsync(HttpRequest request), will contain the logic to follow for the specific grant. 2 authorize type providers are provided by default - AuthorizeTypeProviderAuthorizationCode and AuthorizeTypeProviderImplicit.
Now, in Startup.cs ConfigureServerices(), add a new trasient type that specifies the interface IAuthorizeResponseType and the implementation of the new class created above. This is extremely important as the Authorize controller injects an array of all IAuthorizeResponseType[] classes registered in dependency injection, and if you do not register your new class in Startup.cs ConfigureServices(), it will not be found in the Authorize Controller when executing the Authorize request.
Each IAuthorizeTypeProvider will inject an IAuthorizeBLL (AuthorizeBLL) that should contain the logic required for all AuthorizeResponseType implementations. AuthorizeBLL in turn is dependent on a application DAL, which is by default, dependent on Entity Framework, Asp.NET Identity, and SQL Server.
ValidateAsync() returns a compound result of (bool, string), where the bool is the flag to indicate if validation is successful, and the string represents the error message if the authorization was not successful. A successful validation request will redirect to the Login page provided by OAuthServer.NET.
OAuthServer.NET is written following the OAuth 2.0 protocol, the PKCE extesions, and allows for custom grants. Once your instance of OAuthServer.NET and OAuthServer.NET-UI are running, it is time to configure your server.
Content type
Image
Digest
Size
95.1 MB
Last updated
almost 5 years ago
docker pull liveordevtrying/oauthserver.net