【发布时间】:2018-11-03 00:39:33
【问题描述】:
我有几种不同类型的用户(InternalUser、Clinician 和 Patient)可以登录我的网站,因此,对于每种类型的用户,我都创建了一个继承自 @ 的用户类987654327@.
如果登录用户来到网站的主页 (https://example.com/),则 MVC 控制器代码需要确定用户的类型,以便它可以将重定向返回到正确的“主页”页面 ( https://example.com/clinician、https://example.com/patient 等)。
在我看来,使用声明作为一种简单、低成本的方式来区分控制器中的用户类型,而无需从数据库中加载当前用户,这似乎是合理的。当我注册用户时,我添加了一个代表用户“类型”的声明:
await _userManager.AddClaimAsync("ihi:user_type", "clinician");
然后,在控制器中,我检查声明:
[Authorize]
public IActionResult Index()
{
if (User.HasClaim("ihi:user_type", "clinician")) return Redirect("...");
if (User.HasClaim("ihi:user_type", "patient")) return Redirect("...");
if (User.HasClaim("ihi:user_type", "internal")) return Redirect("...");
throw new InternalErrorException("Logged in user is not in a recognized user role");
}
我有几个关于这个方案的问题:
- 这是一种合理的方法吗?
- 此方案(
[Authorize]属性和我的声明检查)依赖于查看 cookie 的值。存储在该 cookie 中的数据是否相当安全,不会被篡改? (我希望如此,否则攻击者可以修改 cookie 并游戏身份系统。)
【问题讨论】:
标签: c# asp.net-core asp.net-core-identity