C# 在两个asp核心api之间共享Jwt令牌
我这里有麻烦。 我正在尝试构建第二个asp核心api,它将共享一个jwt令牌,该令牌由我的主asp核心api aka:authorization server创建。 但我不能让它工作 这个想法是,我的angular客户端将从我的主api授权服务器获取一个令牌,并发送相同的令牌以从另一个api获取数据,该api必须与auth服务器进行检查,我猜令牌是否有效 我认为这在配置otpion的某个地方:C# 在两个asp核心api之间共享Jwt令牌,c#,asp.net-core,jwt,token,C#,Asp.net Core,Jwt,Token,我这里有麻烦。 我正在尝试构建第二个asp核心api,它将共享一个jwt令牌,该令牌由我的主asp核心api aka:authorization server创建。 但我不能让它工作 这个想法是,我的angular客户端将从我的主api授权服务器获取一个令牌,并发送相同的令牌以从另一个api获取数据,该api必须与auth服务器进行检查,我猜令牌是否有效 我认为这在配置otpion的某个地方: .AddJwtBearer(options =>
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])),
// Ensure the token audience matches our audience value (default true):
ValidateAudience = true,
};
我的配置文件中有:
"Jwt": {
"Key": "METAPRODUCTIQUE2904",
"Audience": "http://localhost:52771/",
"Issuer": "http://localhost:52771",
},
然后在端口52771上运行主应用程序进行测试,在端口52772上运行我当前的辅助应用程序
但我想我做错了什么,但不知道是什么
我非常感谢你们的一些想法首先,让我们看看它是如何工作的
1) user ---------------------(Get Token) ---------------------> AuthServer
2) AuthServer -----------------(Token)------------------------> user
3) user ----------(Request with Token as auth header)---------> OtherServers
在步骤2中,AuthServer检查用户权限并创建一个包含用户信息、权限和其他内容的数据包,然后使用您提供给base64字符串(我们称之为令牌)的密钥对其进行加密
我们在其他服务器上也有密钥。当他们收到请求时,他们首先尝试用密钥解密。如果一切正常,现在另一台服务器拥有用户信息、权限和其他内容
因此,AuthServer不需要连接到其他服务器,它们可以相互协作
在这两个服务的startup类中,您应该具有以下代码:
public void ConfigureServices(IServiceCollection services)
{
...
// JWT
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
.GetBytes("MySpecialKey")),
ValidIssuer = "MainServer",
ValidAudience = "Sample",
ValidateIssuer = false,
ValidateAudience = false
};
});
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseAuthentication();
...
}
在authorization server的一项服务中,您应该实现一种为注册用户生成令牌的方法:
public async Task<string> GenToken()
{
// check if the user has the required permissions
....
// authentication successful so generate jwt token
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, "Username"),
}),
Expires = DateTime.UtcNow.AddDays(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII
.GetBytes("MySpecialKey")), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenString = tokenHandler.WriteToken(token);
return tokenString;
}
现在,您可以在控制器或API上使用[Authorize]属性
每个对[授权]控制器或API的请求都应包含一个带有密钥授权和Bear USERTOKEN值的头文件您使用的是Identity Server吗?否则我建议你这样做。这是一个很好的介绍,其中有一个与您尝试完成的场景类似的场景:文档位于,我已经完成了,但是无法让第二台服务器到达第一台服务器。。。。我们可以通过邮件或在stackoverflow上给我发一条私人消息来交换更多信息吗?我很高兴知道我已经更新了答案,包含了逻辑。服务器不需要连接,因为身份验证正在使用加密来完成此项工作。谢谢amin,但例如,对于ValidisUser=MainServer、Validudience=Sample,您会为每个服务器设置什么值,就像和或只是简单密钥一样?这些值是可选的,您可以设置任何值,关键是,您可以在其他服务器上解密令牌后验证它们。