Session Management in MVC
Asked Answered
C

4

12

I am new in MVC. I am creating new WebApplication in MVC4 Razor. I want to maintain User Login session for all pages. Can any one Explain me how to maintain session for all views in MVC with small example.

Cesura answered 4/10, 2013 at 12:24 Comment(1)
Do you want to show user login on all the pages?Calcutta
C
21

Session management is simple. Session object is available inside MVC controller and in HttpContext.Current.Session. It is the same object. Here is a basic example of how to use Session:

Write

Session["Key"] = new User("Login"); //Save session value

Read

user = Session["Key"] as User; //Get value from session

Answering your question

if (Session["Key"] == null){
   RedirectToAction("Login");
}

Check out Forms Authentication to implement highly secure authentication model.


UPDATE: For newer versions of ASP.NET MVC you should use ASP.NET Identity Framework. Please check out this article.

Calcutta answered 4/10, 2013 at 12:28 Comment(2)
how do i check that session for every views. if session not exist then how to redirect to login form.Cesura
@SanketS: Look in to SimpleMembership, it will do all this for you (and offer the convenience of using the [Authorize] attribute).Pronto
B
4

Here is a Example. Say we want to manage session after checking user validation, so for this demo only I am hard coding checking valid user. On account Login

public ActionResult Login(LoginModel model)
        {
            if(model.UserName=="xyz" && model.Password=="xyz")
            {
                Session["uname"] = model.UserName;
                Session.Timeout = 10;
                return RedirectToAction("Index");
            }
}

On Index Page

public ActionResult Index()
        {
            if(Session["uname"]==null)
            {
                return Redirect("~/Account/Login");
            }
            else
            {
                return Content("Welcome " + Session["uname"]);
            }
        }

On SignOut Button

Session.Remove("uname");
return Redirect("~/Account/Login");
Bahena answered 30/12, 2014 at 14:32 Comment(0)
F
3

Have you worked on Asp.Net application? Using Forms Authentication you can easily maintain user session.

Find the below given links for your reference: http://www.codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF http://msdn.microsoft.com/en-us/library/ff398049(v=vs.100).aspx

Fineberg answered 4/10, 2013 at 12:32 Comment(0)
S
0

Set Session

public IActionResult SetState()
{
    // State Save on 
    string name = "ITI";
    int age = 23;
    HttpContext.Session.SetString("StudentName", name);
    HttpContext.Session.SetInt32("StudentAge", age);

    return Content("Data Saved");
}

Get Session

public IActionResult GetState()
{
    string Name = "NULL";
    int Age = 0;
    if (HttpContext.Session.GetString("StudentName") != null)
    {
        Name = HttpContext.Session.GetString("StudentName");
        Age = (int)HttpContext.Session.GetInt32("StudentAge");
        return Content($"Get Data Success, Name = {Name} \t Age = {Age}");
    }
    return Content($"Get Data Not Success, Name = {Name} \t Age = {Age}");
}
Semiotic answered 26/9, 2023 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.