Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
309 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...