会话管理MVC [英] Session Management in MVC

查看:103
本文介绍了会话管理MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC新。我在MVC4剃刀创造新的WebApplication。我想保持用户登录会话的所有网页。任何一个可以解释我如何为MVC小例子中,所有的观点维持会话。

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.

推荐答案

会话管理简单。 Session对象可用MVC控制器内部,并在 HttpContext.Current.Session 。它是同一个对象。下面是如何使用Session一个基本的例子:

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:

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

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

回答你的问题

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

查看表单验证实现高度安全的身份验证模式。

Check out Forms Authentication to implement highly secure authentication model.

更新:对于ASP.NET MVC的新版本,你应该使用ASP.NET身份框架。请看看这篇文章

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

这篇关于会话管理MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆