JWT Same Application 是 Auth Server 和 Application Server
JWT Same Application is the Auth Server and the Application Server
各位专家,我最近对 OAuth 和 JWT 的工作原理进行了很多探索。本质上应该有一个 AuthServer 发布令牌并且应该有一个 ServiceAPI(Application Server) 使用令牌一个 客户 !!.我还了解到令牌由三部分组成,header、有效负载和签名...
现在,如果我想构建一个 API 来执行...验证和颁发 JWT 令牌,然后提供服务......这听起来像是使用令牌的基本身份验证!!
我也不确定我写的代码是否反映了这个概念(令牌发行者与服务相同API)。我正在按照 this 文章构建 .net core 2.1 Web API。
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://localhost:44387/";
options.Audience = "JWT:Issuer";
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(5);
options.RequireHttpsMetadata = false;
});
services.AddAuthorization(options =>
{
options.AddPolicy("GuidelineReader", p => {
p.RequireClaim("[url]", "GuidelineReader");
});
});
}
我还添加了一个生成令牌的 LoginController 并且 returns 它...
[AllowAnonymous]
[HttpPost]
public IActionResult Login([FromBody]Application login)
{
IActionResult response = Unauthorized();
var user = AuthenticateUser(login);
if (user != null)
{
var tokenString = GenerateJSONWebToken(user);
response = Ok(new { token = tokenString });
}
return response;
}
private string GenerateJSONWebToken(Application appInfo)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
null,
expires: DateTime.Now.AddMinutes(120),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
下面有什么区别
- options.Authority
- options.Audience(我以为是发送https请求的应用)
- options.Issuer
options.Authority
Authority
是发令牌的认证服务器地址。在您的场景中,网络 api 发出令牌,因此 Authority
将是网络 api 的 url 。
options.Audience (I am thinking it is the application that sends https request)
Audience
表示传入令牌的预期接收者或令牌授予访问权限的资源。在您的场景中,网络 api 是客户端将使用 JWT 令牌访问的受保护资源,网络 api 将验证令牌以检查 claims/signature 。所以 web api name/URL 应该是 Audience
options.Issuer
Issuer
标识构造和 return 令牌的安全令牌服务 (STS)。在您的方案中,网络 api 验证用户凭据和 return 令牌。所以网页 api name/URL 是 Issuer
各位专家,我最近对 OAuth 和 JWT 的工作原理进行了很多探索。本质上应该有一个 AuthServer 发布令牌并且应该有一个 ServiceAPI(Application Server) 使用令牌一个 客户 !!.我还了解到令牌由三部分组成,header、有效负载和签名...
现在,如果我想构建一个 API 来执行...验证和颁发 JWT 令牌,然后提供服务......这听起来像是使用令牌的基本身份验证!!
我也不确定我写的代码是否反映了这个概念(令牌发行者与服务相同API)。我正在按照 this 文章构建 .net core 2.1 Web API。
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://localhost:44387/";
options.Audience = "JWT:Issuer";
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(5);
options.RequireHttpsMetadata = false;
});
services.AddAuthorization(options =>
{
options.AddPolicy("GuidelineReader", p => {
p.RequireClaim("[url]", "GuidelineReader");
});
});
}
我还添加了一个生成令牌的 LoginController 并且 returns 它...
[AllowAnonymous]
[HttpPost]
public IActionResult Login([FromBody]Application login)
{
IActionResult response = Unauthorized();
var user = AuthenticateUser(login);
if (user != null)
{
var tokenString = GenerateJSONWebToken(user);
response = Ok(new { token = tokenString });
}
return response;
}
private string GenerateJSONWebToken(Application appInfo)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
null,
expires: DateTime.Now.AddMinutes(120),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
下面有什么区别
- options.Authority
- options.Audience(我以为是发送https请求的应用)
- options.Issuer
options.Authority
Authority
是发令牌的认证服务器地址。在您的场景中,网络 api 发出令牌,因此 Authority
将是网络 api 的 url 。
options.Audience (I am thinking it is the application that sends https request)
Audience
表示传入令牌的预期接收者或令牌授予访问权限的资源。在您的场景中,网络 api 是客户端将使用 JWT 令牌访问的受保护资源,网络 api 将验证令牌以检查 claims/signature 。所以 web api name/URL 应该是 Audience
options.Issuer
Issuer
标识构造和 return 令牌的安全令牌服务 (STS)。在您的方案中,网络 api 验证用户凭据和 return 令牌。所以网页 api name/URL 是 Issuer