2

Below is the code used to login

WebSecurity.Login("abc", "123", true);//return true
return RedirectToAction("afterLogin", @"afterLogin");

After loggin in, I checked the user's id to see if it's -1 by running the below line:

WebSecurity.CurrentUserId

But why whenever I called this, the return value always -1 and CurrentUserName is empty?

edited:

An additional question:

Does the WebSecurity have something like timeout so that the user idle for a specific period and will logged out automatically?

4

2 回答 2

3

检查您的 webconfig,您必须启用表单身份验证:

在里面添加以下代码段

   <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="3600" />
    </authentication>

如果它在您的 webconfig 中,请注释掉:

<!--<modules>
      <remove name="FormsAuthentication" />
</modules>-->

现在你可以检查

WebSecurity.CurrentUserName、WebSecurity.CurrentUserId 和、WebSecurity.IsAuthenticated 标志;

还要在 app_start 中添加这个类

public static class AuthConfig
    {
        public static void RegisterAuth()
        {
 }
}

并在 Global.asax.cs 的 AppStart 中调用它

AuthConfig.RegisterAuth();
于 2015-05-02T02:36:46.027 回答
2

我认为默认到期时间是浏览器会话结束时。可能是未启用 cookie,这就是它返回 -1 需要启用 cookie 的原因。

于 2013-10-28T20:17:56.103 回答