Aloji.AspNetCore.JwtSecurity 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Aloji.AspNetCore.JwtSecurity --version 1.0.1
NuGet\Install-Package Aloji.AspNetCore.JwtSecurity -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aloji.AspNetCore.JwtSecurity" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aloji.AspNetCore.JwtSecurity --version 1.0.1
#r "nuget: Aloji.AspNetCore.JwtSecurity, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aloji.AspNetCore.JwtSecurity as a Cake Addin
#addin nuget:?package=Aloji.AspNetCore.JwtSecurity&version=1.0.1

// Install Aloji.AspNetCore.JwtSecurity as a Cake Tool
#tool nuget:?package=Aloji.AspNetCore.JwtSecurity&version=1.0.1

JwtSecurity

The object is to allow using a token generated in an OWIN OAuth 2.0 Server in AspNet.Core projects.

Nugets: https://www.nuget.org/profiles/aloji

Real life problem

We have the authorization server implemented with OWIN OAuth 2.0, but the new developments are with AspNetCore

The first idea was to use the machine keys

MachineKey

If the authorization server and the resource server are not on the same computer, the OAuth middleware will use the different machine keys to encrypt and decrypt bearer access token. In order to share the same private key between both projects, we add the same machinekey setting in both web.config files.

The problem is that machinekey does not exist in AspNetCore, but MS gives us a compatibility solution to replace the machinekey settings in AspNetWebApi2 and using a key storge provider like redis we can be shared the keys.

After several hours trying to implement this solution, I realized that it was easier, cleaner and cheaper to change the token generator to use JWT and dont use any external provider.

Configuration

How to setup the JwtSecurity in OWIN OAuth 2.0 Server (full sample code)

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
        {
            AccessTokenFormat = new JwtSecureDataFormat(
                new JwtSecurityOptions
                {
                    Issuer = "yourIssuerCode",
                    IssuerSigningKey = "yourIssuerSigningKeyCode"
                })
        });
    }
}

How to setup the JwtSecurity in Resource Server .NetFramework (full sample code)

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
        {
            AccessTokenFormat = new JwtSecureDataFormat(
                new JwtSecurityOptions
                {
                    Issuer = "yourIssuerCode",
                    IssuerSigningKey = "yourIssuerSigningKeyCode"
                })
        });
    }
}

How to setup the JwtSecurity in Resource Server .NetFramework with Owin Auth Compatibility

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
        {
            AccessTokenFormat = new MachineKeyCompatibilityDataFormat(
                new JwtSecurityOptions
                {
                    Issuer = "yourIssuerCode",
                    IssuerSigningKey = "yourIssuerSigningKeyCode"
                })
        });
    }
}

How to setup the JwtSecurity in Resource Server .NetCore (full sample code)

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
              .AddJwtBearerAuthentication(options =>
              {
                  options.Issuer = "yourIssuerCode";
                  options.IssuerSigningKey = "yourIssuerSigningKeyCode";
              });
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
          app.UseAuthentication();
    }
}
Bonus:

I developed a middleware to create a JwtServer with AspNetCore very similar to OwinOAuth2 settings

How to setup the JwtServer in AspNetCore (full sample code)

 public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddJwtServer();
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
          app.UseJwtServer(options => {
                options.TokenEndpointPath = "/token";
                options.AccessTokenExpireTimeSpan = new TimeSpan(1, 0, 0);
                options.Issuer = "yourIssuerCode";
                options.IssuerSigningKey = "yourIssuerSigningKeyCode";
                options.AuthorizationServerProvider = new AuthorizationServerProvider
                {
                    OnGrantResourceOwnerCredentialsAsync = async (context) =>
                    {
                        if (context.UserName != context.Password)
                        {
                            context.SetError("Invalid user authentication");
                            return;
                        }

                        var claims = new List<Claim>
                        {
                            new Claim(ClaimTypes.Surname, context.UserName)
                        };

                        context.Validated(claims);
                        await Task.FromResult(0);
                    }
                };
            });
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 5,338 4/22/2020
1.0.1 2,279 2/6/2019
1.0.0 2,994 8/28/2018