【问题标题】:Are ASP.NET Core claims reliable and secure from tampering?ASP.NET Core 声明是否可靠且安全,不会被篡改?
【发布时间】:2018-11-03 00:39:33
【问题描述】:

我有几种不同类型的用户(InternalUserClinicianPatient)可以登录我的网站,因此,对于每种类型的用户,我都创建了一个继承自 @ 的用户类987654327@.

如果登录用户来到网站的主页 (https://example.com/),则 MVC 控制器代码需要确定用户的类型,以便它可以将重定向返回到正确的“主页”页面 ( https://example.com/clinicianhttps://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");
}

我有几个关于这个方案的问题:

  1. 这是一种合理的方法吗?
  2. 此方案([Authorize] 属性和我的声明检查)依赖于查看 cookie 的值。存储在该 cookie 中的数据是否相当安全,不会被篡改? (我希望如此,否则攻击者可以修改 cookie 并游戏身份系统。)

【问题讨论】:

    标签: c# asp.net-core asp.net-core-identity


    【解决方案1】:

    这是一个合理的方法吗?

    是的,这是一种常见的方法,因为 Identity cookie 包含所有声明。

    存储在该 cookie 中的数据是否相当安全,不会被篡改?

    来自the docs

    SignInAsync 创建一个加密的 cookie 并将其添加到当前 回复。如果不指定 AuthenticationScheme,则默认 使用方案。

    在幕后,使用的加密是 ASP.NET Core 的数据 保护系统。

    如您所见 here,您可以对数据保护系统进行许多配置,包括使用证书创建/保护用于加密身份 cookie 的密钥,以及使用临时密钥以使所有 cookie 永久存在关闭应用程序时丢失。

    总的来说,除非您需要一些高级别的安全性,否则默认设置就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 2018-05-08
      • 2022-08-04
      • 1970-01-01
      • 2010-10-23
      相关资源
      最近更新 更多