mvc Web安全.用户登录后,CurrentUserName为空,CurrentUserID为-1
本文关键字:CurrentUserName 为空 CurrentUserID Web 安全 用户 登录 mvc | 更新日期: 2023-09-27 18:37:10
以下是用于登录的代码
WebSecurity.Login("abc", "123", true);//return true
return RedirectToAction("afterLogin", @"afterLogin");
登录后,我通过运行以下行检查用户的id,看看它是否为-1:
WebSecurity.CurrentUserId
但是,为什么每当我调用它时,返回值总是-1,而CurrentUserName
是空的呢?
编辑:
另一个问题:
WebSecurity
是否有类似超时的功能,以便用户空闲一段特定时间并自动注销?
检查您的webconfig,您必须启用表单身份验证:
在中添加以下代码段
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="3600" />
</authentication>
如果它在你的webconfig:中,请注释掉
<!--<modules>
<remove name="FormsAuthentication" />
</modules>-->
现在你可以检查
网络安全。CurrentUserName,WebSecurity。CurrentUserId和,WebSecurity。IsAuthenticated标志;
也在app_start 中添加此类
public static class AuthConfig
{
public static void RegisterAuth()
{
}
}
并在Global.asax.cs 中的AppStart中调用此
AuthConfig.RegisterAuth();
我认为默认的到期时间是浏览器会话结束时。可能是cookie没有启用,这就是它返回-1 cookie需要启用的原因。