Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/catcherwong/nancy.authentication.jwtbearer
:fireworks:A JwtBearer authentication provider for Nancy.
https://github.com/catcherwong/nancy.authentication.jwtbearer
authentication json-web-token jwt nancy nancyfx
Last synced: 4 months ago
JSON representation
:fireworks:A JwtBearer authentication provider for Nancy.
- Host: GitHub
- URL: https://github.com/catcherwong/nancy.authentication.jwtbearer
- Owner: catcherwong
- License: mit
- Created: 2017-07-16T02:46:17.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-10T03:45:05.000Z (about 7 years ago)
- Last Synced: 2024-10-01T06:58:27.701Z (5 months ago)
- Topics: authentication, json-web-token, jwt, nancy, nancyfx
- Language: C#
- Homepage:
- Size: 62.5 KB
- Stars: 26
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README-zh.md
- License: LICENSE
Awesome Lists containing this project
README
[English](./README.md)
# Nancy.Authentication.JwtBearer
![NuGet Version](https://img.shields.io/nuget/v/Nancy.Authentication.JwtBearer.svg)
[![Build status](https://ci.appveyor.com/api/projects/status/6jeqlrrjh8f5enjy?svg=true)](https://ci.appveyor.com/project/catcherwong/nancy-authentication-jwtbearer)
为Nancy提供JwtBearer授权验证的一个扩展。
这个组件基于.NET Standard 2.0,并且已经可以在NuGet上下载安装了!
NuGet上最新的版本已经同时支持.NET Standard 2.0 和 .NET Framework 4.5.2了。
您可以通过下面的链接访问这个Package相关的信息:
# 快速上手
## 安装NuGet包
```
Install-Package Nancy.Authentication.JwtBearer
```## 在Bootstrapper类中配置JwtBearer相关信息
在这一步主要是配置启用JwtBearer验证,需要实例化一个**JwtBearerAuthenticationConfiguration**对象,这个对象包括了JwtBearer验证的相关配置信息。
最后需要调用IPipelines的扩展方法去启用JwtBearer。
相关配置参数说明
参数名 | 说明
---|---
TokenValidationParameters | Token验证所需要的参数
Challenge | HTTP头部WWW-Authenticate字段的配置示例代码如下:
```csharp
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 = "http://www.c-sharpcorner.com/members/catcher-wong",// 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);
}
}
```## 在Module中添加授权验证
只需要在Module中添加`this.RequiresAuthentication();`即可。
示例代码如下:
```csharp
public class MainModule : Nancy.NancyModule
{
public MainModule()
{
this.RequiresAuthentication();Get("/", _ =>
{
return "From JwtBearer Authentication";
});
}
}
```