【问题标题】:Manage 2 levels of authentication with Asp.Net Identity 2.0使用 Asp.Net Identity 2.0 管理 2 级身份验证
【发布时间】:2014-12-11 15:59:17
【问题描述】:

我希望能够为我的 asp.net 应用用户管理 2 个级别的身份验证。这个想法是用户可以使用“记住我”选项永久登录并访问一些没有非常敏感数据的用户页面,但他们每次想要访问其他页面时都必须重新输入登录名/密码(比如他们的付款信息)。安全页面的这种身份验证仅适用于会话。 当然,我可以通过处理另一个 cookie 并让 Identity 处理永久的 cookie 来手动执行此操作,但也许有一种方法可以仅使用 Identity 来执行此操作。

您可以了解我想通过亚马逊网站实现的目标。您可以永久登录,然后您总是会在网站上以您的名字受到欢迎,您可以看到您的推荐等...但是如果您想访问您的帐户或购买东西,您必须输入您的登录名/密码(然后它仅对会话有效)。

谢谢!

【问题讨论】:

    标签: asp.net authentication asp.net-identity


    【解决方案1】:

    您可以设置两个登录 cookie 来表示这一点,您的正常登录页面使用默认的身份应用程序 cookie。但是您更安全的页面会创建一个不同的 cookie,该 cookie 仅用于您的超级安全区域。

    以下是为此目的添加不同 CookieMiddleware 的方法:

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "SuperSecureMode",
                AuthenticationMode = AuthenticationMode.Passive
            }
    

    在您重新验证凭据后,您可以像这样存储您的 cookie(您至少需要添加用户 ID,以便稍后验证):

    HttpContext.GetOwinContext().Authentication.SignIn(new ClaimsIdentity("SuperSecureMode"));
    

    在您的超级安全页面上,您需要进行身份验证并从声明身份中提取用户 ID。

    HttpContext.GetOwinContext().AuthenticateAsync("SuperSecureMode");
    

    【讨论】:

    • 我没有意识到您可以像这样管理多个身份 cookie,非常感谢!我快到了,当我登录时,我有这个: AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity.Result); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, new ClaimsIdentity("SecureSession"));它按预期发出 2 个 cookie,但两者都是持久性的。我只需要 SecureSession cookie 会话。
    猜你喜欢
    • 2023-04-08
    • 2015-01-20
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2014-06-14
    • 2018-08-21
    相关资源
    最近更新 更多