微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为每个用户身份设置会话

如何解决为每个用户身份设置会话

我在 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 举报,一经查实,本站将立刻删除。