小编C R*_*man的帖子

HttpContext.Session不保持状态

我到处都是网络,以查找使用HttpContext.Session出了什么问题。似乎在设置会话时已在设置会话,但是在离开函数后,我无法访问它。我已经检查了Startup.cs文件,以确保正确添加了所有内容并安装了正确的NuGet软件包。

在我实际使用会话的控制器类中,我有以下代码:

    /// <summary>
    /// Check to see if user exists.  If yes, go to the administrator interface,
    /// else return to the login page.
    /// </summary>
    /// <param name="administrator"></param>
    /// <returns></returns>
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult AdminLogin([Bind("UserName", "Password")] Administrator administrator)
    {
        try
        {
            var admin = _context.Administrators.Where(x => x.UserName == administrator.UserName
                && x.Password == administrator.Password).FirstOrDefault();

            if (admin != null)
            {
                HttpContext.Session.SetString("IsAdmin", "true");
                _httpContextAccessor.HttpContext.Response.Cookies.Append("IsAdmin", "true");
            }

            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Shows …
Run Code Online (Sandbox Code Playgroud)

c# session persistence httpcontext asp.net-core

6
推荐指数
1
解决办法
4851
查看次数

标签 统计

asp.net-core ×1

c# ×1

httpcontext ×1

persistence ×1

session ×1