我需要在成功登录后直接获取 UserId Guid。以下代码不起作用:
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// doesn't run
Guid puk = (Guid)Membership.GetUser().ProviderUserKey;
}
}
以下代码确实有效:
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
MembershipUser user = Membership.GetUser(txtUsername.Value);
if (user != null)
{
Guid puk = (Guid)user.ProviderUserKey;
}
}
为什么会这样?除此之外还有什么事情要做SetAuthCookie
吗?