为每个用户身份设置会话

问题描述

我在 ASP.NET MVC 中有一个简单的页面 View

@HttpContext.Current.Session["User"].ToString() @DateTime.Now

我只是想以编程方式设置用户的身份,这样当我重新导航到此页面或任何其他页面时,我可以获得当前的用户身份。

此外,当导航新用户时,我需要创建一个新的用户身份。

这是控制器

[HttpGet]
[OutputCache(NoStore = true,Duration = 60,varyByParam = "*")]
public ActionResult Index()
{
    var pModel = new PModel
    {
        Countries = PopulateDropDown(" SELECT ... ")
    };

    HttpContext.Session["User"] = HttpContext.User.Identity.Name.ToString();
    HttpContext.Session.Timeout = 300;

    return View(pModel);
}

这是打印在页面 View 上的值,如果我从台式机连接到应用程序

myDomain\myUser 22/01/2021 19:53:15

现在我尝试从我的移动设备连接到新用户的应用程序,页面上打印的值 View

myDomain\myUser2 22/01/2021 20:04:37

但是在我的桌面电脑上刷新浏览器时,页面上打印的值View 更改为

myDomain\myUser2 22/01/2021 20:08:25

用户 myUser2 在应用上取代了第一个连接的用户 myUser

如果我反向移动也会发生同样的情况,即移动设备与台式电脑...

对此有特殊考虑吗?

在 ASP.NET MVC 项目中使用没有问题...对于每个用户我都有相应的会话而无需替换用户...

我不明白我的 ASP.NET MVC 应用程序中的这个问题...对不起...

public static string tUser
{
    get
    {
        if (HttpContext.Current.Session["tUser"] != null)
        {
            return HttpContext.Current.Session["tUser"].ToString();
        }
        return null;
    }
    set
    {
        HttpContext.Current.Session["tUser"] = value;
    }
}

更新 #1

enter image description here

更新 #2

[HttpGet]
[OutputCache(NoStore = true,varyByParam = "*")]
public ActionResult Index()
{
        System.Web.HttpContext.Current.Session["User"] = HttpContext.User.Identity.Name.ToString();
        System.Web.HttpContext.Current.Session.Timeout = 300;

    var pModel = new PModel
    {
        Countries = PopulateDropDown(" SELECT ... ")
    };

    return View(pModel);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)