1

我正在使用 WebSecurity 和 SimpleMembershipProvider 来登录用户。

用户可以更改他们的电子邮件

Dim memberId As Integer = 1

Dim context As UsersContext = New UsersContext
Dim userProfile As UserProfile =
    context.UserProfiles.Where(Function(f) f.UserId = memberId).SingleOrDefault()
' Email before the change: "a@a.com"
userProfile.UserName = "b@b.com"
context.SaveChanges()

但是,在此更新之后,HttpContext 仍将用户报告为他们的旧电子邮件。

' Name is "a@a.com" but should be "b@b.com"
HttpContext.User.Identity.Name

起初我以为我可以将用户注销并重新登录

WebSecurity.Logout()
' but I don't have the user's password
WebSecurity.Login("b@b.com", "???")

如何以某种方式刷新身份验证 cookie 以反映用户更改其登录详细信息?

4

2 回答 2

1

在这里。这是一种(丑陋的?)方法:

Dim newEmail As String = "b@b.com"

Dim ticket As FormsAuthenticationTicket =
    New FormsAuthenticationTicket(newEmail, False, FormsAuthentication.Timeout.Minutes)

Dim identity As IIdentity = New FormsIdentity(ticket)

HttpContext.Current.User = New RolePrincipal(identity)

FormsAuthentication.SetAuthCookie(newEmail, False)

它工作正常,但它似乎有点难看。它迫使我的应用程序依赖FormsAuthentication(正确?)

@戴夫一个

您认为我应该使用此代码,但将其放入我自己的自定义MemershipProvider中吗(对吗?是MembershipProvider处理这个的吗?)

于 2013-04-02T13:18:20.360 回答
1

要通过成员资格更改 cookie,您似乎坚持将用户注销并重新登录。

此时您面临的困境是如何在没有用户密码的情况下登录用户

最好的前景似乎是在用户名更改期间询问用户的密码。这有一种合理的感觉,并用密码武装你。

于 2013-04-02T23:28:58.233 回答