0

I've got a simple page View in ASP.NET MVC with

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

I simply want to programmatically set the user's identity so that when I re-navigate to this page or any other I can get the current user identity.

Also when navigate new User I need create a new User identity.

This is the controller

[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);
}

This is the value printed on the page View if I'm connected to app from desktop pc

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

Now I have tried connected to app from my mobile device with new user and the value printed on the page View is

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

But when refresh the browser on my desktop pc value printed on the page View is changed to

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

The user myUser2 replaced on the app the first connected user myUser

The same happens if I move in reverse, that is mobile device vs desktop pc...

Are there special considerations for this?

In the ASP.NET MVC project using without problem... for each user I have the corresponding Session without replace user...

I don't understand this problem in my ASP.NET MVC application... I'm sorry...

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;
    }
}

Update #1

enter image description here

Update #2

[HttpGet]
[OutputCache(NoStore = true, Duration = 60, 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);
}
Hamamelis
  • 1,983
  • 8
  • 27
  • 41
  • HttpContext.Current - that's in your second code block and not the first. You want that "Current" `HttpContext.Session["User"] = HttpContext.User.Identity.Name.ToString();` – Nikki9696 Jan 22 '21 at 19:22
  • I think you need the Current in your other spot too `HttpContext.Session` – Nikki9696 Jan 22 '21 at 19:25
  • @Nikki9696 Thank you for reply. Please see **Update #1** on the first question... can't add the `Current` to `HttpContext.Session ` – Hamamelis Jan 22 '21 at 19:27
  • using System.Web - https://stackoverflow.com/questions/18309239/cant-access-to-httpcontext-current/18309281 Note: the answer shows a better way to get to it also. I'm just used to Current =) I'm not sure that's the issue though, looking through that thread. – Nikki9696 Jan 22 '21 at 19:42
  • What happens if you remove that outputcache attribute? It has vary by param, but there's no params passed, so I'm not sure how that might affect things. – Nikki9696 Jan 22 '21 at 19:46
  • @Nikki9696 Please see **Update #2** on the first question. I have changed the `controller` without success. The user `myUser2` replaced on the app the first connected user `myUser` and viceversa... – Hamamelis Jan 22 '21 at 19:59
  • From update #2 - you removed this entire line? `[OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]` – Nikki9696 Jan 22 '21 at 20:00
  • @Nikki9696 Many thanks for this help. I have removed the line` [OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]` and now the user is not replaced!!! – Hamamelis Jan 22 '21 at 20:20
  • 1
    @Nikki9696 Many thanks for this help. I have removed the line `[OutputCache(NoStore = true, Duration = 60, VaryByParam = "*")]` and now the user is not replaced!!! – Hamamelis Jan 22 '21 at 20:26

0 Answers0