为什么Request.IsAuthenticated返回true但session为null

时间:2014-04-22 03:40:17

标签: c# asp.net asp.net-mvc session

在我的ASP.NET MVC项目中。我使用名为 Office 的区域。

Area
|--Office
     |--Controllers
           |--AccountController
                 |-- Methods: Index (authorized)

Controllers
|--HomeController
     |-- Methods: Index (not required authorized)

项目中的共享文件夹(不在区域中)包含:_LoginPartial.chtml,_Layout.chtml(使用_LoginPartial.chtml)

这是_LoginPartial.chtml中的代码:

@if (Request.IsAuthenticated)
{
    <div class="dropdown">
        <span data-toggle="dropdown">
            <span><b>Hello</b></span>
            <span><b>@User.Identity.Name</b></span>
        </span>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
            <li role="presentation">
                <a role="menuitem" tabindex="-1" href="/Office/Account/Password">Change password</a>
            </li>
        </ul>
    </div>
    <span>.</span>
    using (Html.BeginForm("Logoff", "Home", FormMethod.Post, new { id = "logoffForm" })) {
        @Html.AntiForgeryToken()
    <a href="javascript:document.getElementById('logoffForm').submit()">Exit</a>
    }
}
else {
    <text>Log In</text>
}

我使用该方法获取用户的会话,如果session为null,则重定向到登录页面。 BaseController中的代码

protected Account Account
{
    get
    {
         bool flag = true;
         if (Session["User"] == null)
             flag = this.ReLogin();

         if (flag == false) return null;

         Account account = (Account)Session["User"];
         return account;             
    }
}

protected bool ReLogin()
{
    HttpCookie cookieUserName = Request.Cookies["username"];
    if (cookieUserName == null)
        return false;

    HttpCookie cookiePassword = Request.Cookies["password"];
    if (cookiePassword == null)
        return false;

    string username = cookieUserName.Value;
    string password = cookiePassword.Value;
    Account account = this.LoginWithEncrypt(username, password);
    this.SetSession(account);

    return account != null;
}

private void SetSession(Account account)
{
    if (account == null || account.IsSuspend == true)
        return;

    Session["User"] = account;

    this.SetCookie("username", account.Code);
    this.SetCookie("password", account.Password);

}

protected Account Login(string username, string password)
{
    AccountBL accountBL = new AccountBL();
    Account account = accountBL.LoadByCodeAndPassword(username, password);
    this.SetSession(account);

    return account;
}

protected void Logout()
{
    FormsAuthentication.SignOut();
    Session["User"] = null;

    this.RemoveCookie("username");
    this.RemoveCookie("password");
}

这是Office区域中HomeController(继承自BaseController)的代码登录:

[HttpPost]
public ActionResult Login(string username, string password, string returnUrl)
{
    Account account = this.Login(username, password);

    if (account == null)
    {
         ViewBag.Error = "1";
         return View();
    }
    else
    {
         FormsAuthentication.SetAuthCookie(account.Code, true);

         if (string.IsNullOrWhiteSpace(returnUrl))
             return RedirectToAction("Index");
         else
             return Redirect(returnUrl);
    }
}

我转到localhost / Home / Index,它显示用户名。之后,我转到localhost / Office / Account / Index,它说我没有登录,所以将我重定向到登录页面。
我的问题:如果Request.IsAuthenticated为true,但我的会话为空。它有些不对劲,我无法弄明白。
我尝试设置FormsAuthentication.SetAuthCookie(account.Code,true),当注销时,我删除所有cookie并设置FormsAuthentication.SignOut。但它不起作用。
我的目标:如果显示用户名,则该用户在网站上有会话。


如果您不清楚,请在下面评论,我将对其进行编辑。


感谢

0 个答案:

没有答案