成功登录后,User.Identity.IsAuthenticated为false

我需要在成功登录后直接获取用户ID Guid。以下代码不起作用:

if(Membership.ValidateUser(txtsername.Value,txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value,true);
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
//不跑
Guid puk=(Guid)Membership.GetUser().ProviderUserKey;
}
}

以下代码不起作用:

if(Membership.ValidateUser(txtsername.Value,txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value,true);
MembershipUser=Membership.GetUser(txtUsername.Value);
如果(用户!=null)
{
Guid puk=(Guid)user.ProviderUserKey;
}
}

为什么会发生这种情况?除了SetAuthCookie,还有什么事要做吗

我也有同样的问题。我忘了设置web.config配置

也许你也错过了

<系统.web>
<身份验证模式=“表单”>
<forms loginUrl=“~/user/login”timeout=“1000”name=“\uu Auth”/>
&lt/认证>
&lt/系统.web>

发表评论