Ocelot.JWTAuthorize
0.0.310
This library is used in the verification project when Ocelot is used as an API gateway. In the Ocelot project, the API project, the verification project, and the injection function can be used.
See the version list below for details.
Install-Package Ocelot.JWTAuthorize -Version 0.0.310
dotnet add package Ocelot.JWTAuthorize --version 0.0.310
<PackageReference Include="Ocelot.JWTAuthorize" Version="0.0.310" />
paket add Ocelot.JWTAuthorize --version 0.0.310
Ocelot.JWTAuthorize
This library is used in the verification project when Ocelot is used as an API gateway. In the Ocelot project, the API project, the verification project, and the injection function can be used.
1. add the following sections to the appsetting. Json file for each project
{
"JwtAuthorize": {
"Secret": "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
"Issuer": "ocelot",
"Audience": "everyone",
"PolicyName": "permission",
"DefaultScheme": "Bearer",
"IsHttps": false,
"RequireExpirationTime": true
}
}
2. API Project
PM>Install-Package Ocelot.JWTAuthorize
Startup.cs,In ConfigureServices method
services.AddApiJwtAuthorize((context) =>
{
return true;//validate permissions return(permit) true or false(denied)
});
API Controller, "permission" is PolicyName of appsettion.json
[Authorize("permission")]
public class ValuesController : Controller
3. Authorize Project
PM>Install-Package Ocelot.JWTAuthorize
startup.cs,In ConfigureServices method
services.AddTokenJwtAuthorize();
LoginController.cs _tokenBuilder is dependency injection in AddTokenJwtAuthorize,so it's ITokenBuilder
[HttpPost]
public IActionResult Login([FromBody]LoginModel loginModel)
{
if (loginModel.UserName == "gsw" && loginModel.Password == "111111")
{
var claims = new Claim[] {
new Claim(ClaimTypes.Name, "gsw"),
new Claim(ClaimTypes.Role, "admin")
};
//DateTime.Now.AddSeconds(1200) is expiration time
var token = _tokenBuilder.BuildJwtToken(claims, DateTime.Now.AddSeconds(1200));
return new JsonResult(new { Result = true, Data = token });
}
else
{
return new JsonResult(new
{
Result = false,
Message = "Authentication Failure"
});
}
}
4. Ocelot Project
PM>Install-Package Ocelot.JWTAuthorize
Startup.cs,In ConfigureServices method
services.AddOcelotJwtAuthorize();
Ocelot.JWTAuthorize
This library is used in the verification project when Ocelot is used as an API gateway. In the Ocelot project, the API project, the verification project, and the injection function can be used.
1. add the following sections to the appsetting. Json file for each project
{
"JwtAuthorize": {
"Secret": "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
"Issuer": "ocelot",
"Audience": "everyone",
"PolicyName": "permission",
"DefaultScheme": "Bearer",
"IsHttps": false,
"RequireExpirationTime": true
}
}
2. API Project
PM>Install-Package Ocelot.JWTAuthorize
Startup.cs,In ConfigureServices method
services.AddApiJwtAuthorize((context) =>
{
return true;//validate permissions return(permit) true or false(denied)
});
API Controller, "permission" is PolicyName of appsettion.json
[Authorize("permission")]
public class ValuesController : Controller
3. Authorize Project
PM>Install-Package Ocelot.JWTAuthorize
startup.cs,In ConfigureServices method
services.AddTokenJwtAuthorize();
LoginController.cs _tokenBuilder is dependency injection in AddTokenJwtAuthorize,so it's ITokenBuilder
[HttpPost]
public IActionResult Login([FromBody]LoginModel loginModel)
{
if (loginModel.UserName == "gsw" && loginModel.Password == "111111")
{
var claims = new Claim[] {
new Claim(ClaimTypes.Name, "gsw"),
new Claim(ClaimTypes.Role, "admin")
};
//DateTime.Now.AddSeconds(1200) is expiration time
var token = _tokenBuilder.BuildJwtToken(claims, DateTime.Now.AddSeconds(1200));
return new JsonResult(new { Result = true, Data = token });
}
else
{
return new JsonResult(new
{
Result = false,
Message = "Authentication Failure"
});
}
}
4. Ocelot Project
PM>Install-Package Ocelot.JWTAuthorize
Startup.cs,In ConfigureServices method
services.AddOcelotJwtAuthorize();
Dependencies
-
.NETStandard 2.0
- Microsoft.AspNetCore.Authentication (>= 2.1.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 2.1.1)
- Microsoft.AspNetCore.Authorization (>= 2.1.1)
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Ocelot.JWTAuthorize:
Repository | Stars |
---|---|
migomiddle/xms
基于.netcore的跨平台应用框架,包含众多常用模块,易上手、易扩展,xms可理解为x(可扩展的/任意的)m(管理)s(系统)
|
|
axzxs2001/Asp.NetCoreExperiment
原来所有项目都移动到**OleVersion**目录下进行保留。新的案例装以.net 5.0为主,一部分对以前案例进行升级,一部分将以前的工作经验总结出来,以供大家参考!
|