代码之家  ›  专栏  ›  技术社区  ›  HumbleBeginnings

如何在禁用用户的2fa后正确删除.aspnet.twofactorrememberbrowser cookie?

  •  0
  • HumbleBeginnings  · 技术社区  · 6 年前

    Microsoft.aspnet.Identity 2.2版: 当用户禁用2FA时(从以前启用) 用户的cookie .AspNet.TwoFactorRememberBrowser 残余 考虑到权利的限制,可能会造成安全风险。我正在寻找一种干净、适当的方法来删除该用户的cookie,或者我应该将过期日期改为过去的某个日期—如果是这样,我该怎么做?我在谷歌上搜了一大串都没有用,就好像没人知道饼干还在。

    1 回复  |  直到 6 年前
        1
  •  0
  •   HumbleBeginnings    6 年前

    因此,在没有更好的方法的情况下,看起来这将为异步函数提供帮助。 /管理/禁用事实验证 . 注意 IsPersistent=真 移除cookie IsPersistent=假 只需设置过期日期。

    ' POST: /Manage/DisableTwoFactorAuthentication
    <HttpPost>
    <ValidateAntiForgeryToken>
    Public Async Function DisableTwoFactorAuthentication() As Task(Of ActionResult)
        Await UserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), False)
        Dim userInfo = Await UserManager.FindByIdAsync(User.Identity.GetUserId())
        If userInfo IsNot Nothing Then
            Await SignInManager.SignInAsync(userInfo, isPersistent:=False, rememberBrowser:=False)
            Dim rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(userInfo.Id)
            AuthenticationManager.SignIn(New AuthenticationProperties With {
                .IsPersistent = True,   'False still leaves old cookie but with expired date
                .ExpiresUtc = Date.UtcNow.AddDays(-1)
            }, rememberBrowserIdentity)
        End If
        Return RedirectToAction("Index", "Manage")
    End Function
    

    希望这能帮助别人!-)