Asp.net 如何在更改用户角色后更改Context.User

Asp.net 如何在更改用户角色后更改Context.User,asp.net,vb.net,asp.net-membership,asp.net-identity,asp.net-roles,Asp.net,Vb.net,Asp.net Membership,Asp.net Identity,Asp.net Roles,我正在创建和审查新用户。用户注册后,会向他们的电子邮件发送一个链接(包含带有令牌的查询字符串),以便他们可以验证自己的电子邮件地址。当用户单击链接时,他们将被重定向到验证其信息的页面,然后将其角色从来宾更改为成员 工艺流程 电子邮件>verifyEmail.aspx>dashboard.aspx 当用户已经登录到web应用程序,并且他们从电子邮件中单击链接时,他们的角色会相应地改变;但是,当它们重定向到dashboard.aspx时,User.IsInRole(“成员”)为false。注销后,再

我正在创建和审查新用户。用户注册后,会向他们的电子邮件发送一个链接(包含带有令牌的查询字符串),以便他们可以验证自己的电子邮件地址。当用户单击链接时,他们将被重定向到验证其信息的页面,然后将其角色从来宾更改为成员

工艺流程

电子邮件>verifyEmail.aspx>dashboard.aspx

当用户已经登录到web应用程序,并且他们从电子邮件中单击链接时,他们的角色会相应地改变;但是,当它们重定向到dashboard.aspx时,User.IsInRole(“成员”)为false。注销后,再重新登录,User.IsInRole(“成员”)为true。所以我的问题是,如何更新用户的身份,以及用户的上下文,而不强制他们注销然后重新登录?我猜这与角色的cookie有关

代码

    If userToken.Token1 = token Then
      Dim userRole = Roles.GetRolesForUser(authUser)
      Dim userIdentity = New GenericIdentity(authUser)
      Dim principal = New GenericPrincipal(userIdentity, userRole)
      Dim isOnline As Boolean = False

      If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.User.Identity.IsAuthenticated Then
        If Not Membership.GetUser.ProviderUserKey Is Nothing Then
          isOnline = True
        End If
      End If

      Context.User = principal

      If User.IsInRole("Guest") = True AndAlso User.IsInRole("Member") = False Then
        Roles.AddUserToRole(User.Identity.Name, "Member")
        Roles.RemoveUserFromRole(User.Identity.Name, "Guest")

        If isOnline = True Then
          '***do stuff here to change the context
          Response.Redirect("../Account/GetStarted.aspx")
        End If
      End If
    End If

假设您使用的是表单身份验证,则可能需要使用以下方法:

FormsAuthentication.SetAuthCookie
这将“为提供的用户名创建一个身份验证票证,并将其添加到响应的cookies集合中,或者添加到URL(如果您使用的是无Cookie身份验证)。”


取自MSDN

是,表单验证。我已经尝试了FormsAuthentication.SetAuthCookie(User.Identity.Name,False),但没有成功。我没有使用cookieless身份验证。您是否尝试使用其他浏览器?用telerik fiddler来分析你的饼干,当然不,但我现在会的。谢谢你的推荐Roles.Clear()最终成为我所需要的,但是你的回答让我找到了我的答案,所以你得到了支票