'How to type cast of a session object into a class object in c#

i have a class called user. I put that class object into session. My question is, how can i retrieve that object into another page?

var user = obj.user;
var username = obj.username;
var name = obj.cn;
var title = obj.title;
var department = obj.department;
User obj= new User();
Session["User"] = obj;

Another Page:

Index.cshtml

<div>
<h4> Hi @  i need the username here </h4>
<h4> i need department here</h4>
</div>

help me to access the session object.



Solution 1:[1]

In your view,use

  @using Microsoft.AspNetCore.Http;
  <h4>@Context.Session.GetString("User")</h4>

or you can pass Session["User"] into a TempData variable and catch that TempData variable into your index method .so that you can access it into your view.

Solution 2:[2]

I am sure you have found the solution but if not you can simple do this step at the top of the view

`@{ User u = (User)Session["User"];}`

then on page you can simple call any where like Full Name: @u.FullName

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shafiqul Bari Sadman
Solution 2 khurram