asp.net-mvc – jwt令牌多租户
发布时间:2020-12-16 09:32:49 所属栏目:asp.Net 来源:网络整理
导读:我通过令牌在我的asp网络核心项目WebAPI验证中实现,我在每次登录时创建一个令牌,一切似乎都有效. 我的问题是该应用程序将是多个我,所以我将有许多子域客户端 (Client1.myapp.com,client2.myapp.com,client3.myapp.com) 服务器端我管理蜜蜂的应用程序将接受一
我通过令牌在我的asp网络核心项目WebAPI验证中实现,我在每次登录时创建一个令牌,一切似乎都有效.
我的问题是该应用程序将是多个我,所以我将有许多子域客户端 服务器端我管理蜜蜂的应用程序将接受一个参数,该参数将成为租户的名称.
现在,如果我从client1创建令牌,并且我调用了apimyapp.com/client2/api/users(在client1生成的令牌中插入标头,但是调用client2) 我验证令牌. 相反,我希望令牌仅对生成它的租户有效. 在我的startup.cs中: app.UseJwtBearerAuthentication(new JwtBearerOptions() { AutomaticAuthenticate = true,AutomaticChallenge = true,TokenValidationParameters = new TokenValidationParameters() { ValidIssuer = _config["Tokens:Issuer"],ValidAudience = _config["Tokens:Audience"],ValidateIssuerSigningKey = true,IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"])),ValidateLifetime = true } }); 并在我的控制器中生成令牌: var userClaims = _userManagerRepository.GetClaims(user); var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub,user.UserName),new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString()),new Claim(JwtRegisteredClaimNames.GivenName,new Claim(JwtRegisteredClaimNames.FamilyName,new Claim(JwtRegisteredClaimNames.Email,user.Email) }.Union(userClaims); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"])); var creds = new SigningCredentials(key,SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: _config["Tokens:Issuer"],audience: _config["Tokens:Audience"],claims: claims,expires: DateTime.UtcNow.AddMinutes(90),signingCredentials: creds ); 解决方法
您可以在密钥,受众等上添加列表,如TokenValidationParameters
ValidAudiences = new List<string> { "AUDIENCE1","AUDIENCE2" } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 开发和测试环境最佳实践?
- asp.net – 使用R从RSS提要中提取更多帖子到CSV
- asp.net – 如何在Visual Studio中添加NUnit
- asp.net-mvc – 认证之前调用的ASP.NET MVC控制器构造函数
- asp.net-core-1.0 – ASP.NET Core 1.0中的package.json
- asp.net-web-api – SignalR响应覆盖标头
- .net – appSettings等效于applicationSettings的文件属性
- asp.net-mvc – 为什么ASP.NET MVC 4与IList for editor不能
- asp.net-mvc – MVC发布IPagedList
- asp.net-mvc-4 – ASP.NET MVC 4会员“记住我”如何运作?
推荐文章
站长推荐
- asp.net-mvc – 从扩展Apicontroller的MVC控制器
- asp.net – 是否可以在没有Javascript的情况下进
- asp.net-mvc – ActionLink无法在“if”块内渲染
- asp.net-mvc – 在Ubuntu 16.04中运行ASPNET Cor
- asp.net-mvc – 从Visual Studio 2015中完全删除
- asp.net-mvc – RedirectToAction不工作
- asp.net-mvc-4 – 带有TextBoxFor的MVC在循环中具
- asp.net-mvc – 传递子对象问题的嵌套部分视图
- asp.net-mvc – 不要在ASP .NET MVC 4 BundleCon
- asp.net-mvc-3 – 使用参数清理URL以获取MVC索引
热点阅读