【问题标题】:Security check an User to a access controller action对访问控制器操作的用户进行安全检查
【发布时间】:2014-03-18 12:51:14
【问题描述】:

大家好,我有一个设置了 Authorize 属性的 MVC 操作。对于这些操作中的每一个,都有一个仅对该操作有效的密码/安全密码。

public ActionResult Action_1()// generic pin 1
{
Return RedirectToAction("PinCheck", new { returnUrl = "Action_1" });
...
}
[Authorize]
public ActionResult Action_2()// generic pin 2
{
...
}

[Authorize]
public ActionResult PinCheck(string returnUrl)// generic pin 1
{
// request three characters of the pin in random.
...
}
[Authorize]
[HttpPost]
public ActionResult PinCheck(string a, string b, string c, string returnUrl)// generic pin 1
{
// check the three chars.
...
// How do I store pin check for the controller was a success and don't ask the user unless he closes browser or logout
}

我的行动计划是检查管理员为特定用户存储在数据库中的特定操作的 pin。到目前为止,我已经实现了检查PinCheck() 例程,但我面临的问题是用户每次请求特定操作时都必须输入密码。我通过在PinChecksuccess 上保存一个加密的 cookie 来解决这个问题。但是有没有办法修改Authorize 属性和身份验证cookie 本身来实现我在做什么?

【问题讨论】:

    标签: asp.net-mvc cookies asp.net-mvc-5


    【解决方案1】:

    您还可以将已验证的每个 Pin 图表示为存储为 cookie 中 ClaimsIdentity 的一部分的声明,这样您就可以针对用户的声明进行查询,以在每个操作中查找适当的 PinClaim。如果您使用的是 ASP.NET 标识,则可以在验证 pin 时执行以下操作:

    await manager.AddClaimAsync(User.Identity.GetUserId(), new Claim("<mypinclaim>", "<value>"))
    await SignInAsync() // And then resign the user in to regenerate the cookie with the claim
    

    【讨论】:

      【解决方案2】:

      执行此操作的一种方法是创建自定义角色提供程序。您可以通过从RoleProvider 继承来创建一个。然后覆盖IsUserInRole 和可选的FindUsersInRoleGetAllRolesGetUsersInRole 以反映您的引脚管理逻辑。 完成后,通过 web.config 注册自定义角色。

      一篇关于自定义角色提供者的好文章 (http://bojanskr.blogspot.com.au/2011/12/custom-role-provider.html)

      【讨论】:

        猜你喜欢
        • 2014-08-24
        • 2015-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 2023-03-14
        • 1970-01-01
        相关资源
        最近更新 更多