Skip to content

catcherwong/Nancy.Authentication.JwtBearer

Repository files navigation

中文版

Nancy.Authentication.JwtBearer

NuGet Version

Build status

Nancy.Authentication.JwtBearer is a JwtBearer authentication provider for Nancy.

The newest version in NuGet support .NET Standard 2.0 and .NET Framework 4.5.2.

Visit this package in nuget :

https://www.nuget.org/packages/Nancy.Authentication.JwtBearer

Quick Start

Instatll the package at first

Install-Package Nancy.Authentication.JwtBearer

Configure JwtBearer In Bootstrapper Class

public class Bootstrapper : Nancy.DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        var keyByteArray = Encoding.ASCII.GetBytes("Y2F0Y2hlciUyMHdvbmclMjBsb3ZlJTIwLm5ldA==");
        var signingKey = new SymmetricSecurityKey(keyByteArray);

        var tokenValidationParameters = new TokenValidationParameters
        {
            // The signing key must match!
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = signingKey,

            // Validate the JWT Issuer (iss) claim
            ValidateIssuer = true,
            ValidIssuer = "https://github.com/hwqdt/",

            // Validate the JWT Audience (aud) claim
            ValidateAudience = true,
            ValidAudience = "Catcher Wong",

            // Validate the token expiry
            ValidateLifetime = true,

            ClockSkew = TimeSpan.Zero
        };

        var configuration = new JwtBearerAuthenticationConfiguration
        {
            TokenValidationParameters = tokenValidationParameters,
            Challenge = "Guest"//if not use this,default to Bearer
        };

        pipelines.EnableJwtBearerAuthentication(configuration);
    }
}

Add RequiresAuthentication in your modules

public class MainModule : Nancy.NancyModule
{
    public MainModule()
    {
        this.RequiresAuthentication();

        Get("/", _ => 
        {
            return "From JwtBearer Authentication";
        });
    }
}

About

🎆A JwtBearer authentication provider for Nancy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published