.Net-在同一IIS上的两个webapi之间共享令牌
我有一个基于Angular2的项目用于前端,一个基于.NETWebAPI 2.0的项目用于后端 现在我必须实现一个并行项目,该项目必须使用相同的身份验证 我想做的是创建一个新的webapi 2.0并在两个webapi之间共享令牌,以避免用户在项目之间切换时提供相同的用户和密码 可能吗 另一个解决方案是对project2使用相同的Webapi并扩展控制器/方法,但我想为它创建一个新项目.Net-在同一IIS上的两个webapi之间共享令牌,.net,asp.net-web-api2,bearer-token,.net,Asp.net Web Api2,Bearer Token,我有一个基于Angular2的项目用于前端,一个基于.NETWebAPI 2.0的项目用于后端 现在我必须实现一个并行项目,该项目必须使用相同的身份验证 我想做的是创建一个新的webapi 2.0并在两个webapi之间共享令牌,以避免用户在项目之间切换时提供相同的用户和密码 可能吗 另一个解决方案是对project2使用相同的Webapi并扩展控制器/方法,但我想为它创建一个新项目 多亏了支持,如果应用程序托管在两个不同的环境中,它将被视为两个不同的应用程序,但是引入了负载平衡器来处理多个环境
多亏了支持,如果应用程序托管在两个不同的环境中,它将被视为两个不同的应用程序,但是引入了负载平衡器来处理多个环境 关于.net: Out Proc会话是您应该寻找的会话,web应用程序和应用程序都需要一个公共层来保存令牌,它可以是DB、共享文件等,但必须是公共层 我可以使用的另一种方法是,每次向其传递特定参数集时都生成相同的令牌
对于这两种方法,您必须创建一个公共层。如果两个API上的JWT签名密钥相同,并且您关闭了validate Issuer,则令牌将在两个API上都有效 范例
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false, //this needs to be false
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("fjboJU3s7rw2Oafzum5fBxZoZ5jihQRbpBZcxZFd/gY="))
};
});
创建一些声明(角色只是字符串列表)
环境是一样的,领域也是一样的。。。它只是在同一IIS上的两个类似应用程序。。。如何在webconfig中管理MachineKey?您提到了相同的用户和密码,因此如果有一个用户进入,您将导航到托管应用程序中的任何一个,我所说的是一个公共层,可以是任何algo、DB、文件,即使是一个可以发送用户ID密码并生成令牌的不同web服务,我已经有了一个实现.net标识的数据库,我只需要在应用程序之间共享令牌
public List<Claim> CreateJwtClaims( String UserId, List<String> Roles)
{
// create a list of claims and add userId
var Claims = new List<Claim>
{
new Claim(ClaimTypes.Name, UserId)
};
// add roles to the claims list
// for every role add a new claim type role
foreach (var role in Roles)
{
Claims.Add(new Claim(ClaimTypes.Role, role));
}
return Claims;
}
public string CreateToken(List<Claim> Claims)
{
// JWT Key must be the same as in startup
string JwtSigningKey = "fjboJU3s7rw2Oafzum5fBxZoZ5jihQRbpBZcxZFd/gY=";
// create a security key
var Key = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(JwtSigningKey));
// sign the key using specified algorithm
var Creds = new SigningCredentials(Key, SecurityAlgorithms.HmacSha256);
// create the token from the signed credentials
var Token = new JwtSecurityToken(
issuer: "localhost", // or try setting issues the the same from both
audience: "localhost",
claims:Claims // passed claims
expires: DateTime.Now.AddMinutes(10), //expires in 10 mins
signingCredentials: Creds);
return new JwtSecurityTokenHandler().WriteToken(Token);
}
[Authorize] // this says you must have a valid token
// [Authorize(Roles = "admin")] and must be in role admin created in claims
[Route("api/[controller]")]
public class ValuesController : Controller