【问题标题】:MVC Routing and sessionMVC 路由和会话
【发布时间】:2016-09-27 22:38:08
【问题描述】:

我正在开发一个带有 Windows 身份验证的 MVC 应用程序。我们有一个为 windows auth 开发的登录模块,因此当任何用户尝试访问此应用程序时,都会被重定向到 windows auth 的登录页面,托管在其他地方。验证用户后,它会返回用户信息,在这里我正在为用户创建会话。这很好用。

现在考虑两种情况 1. 如果用户尝试访问 http://localhost:63145/Home/Index ,他将被重定向到登录页面,并将控制权返回给我创建会话的 Home Controller 的 Index 方法。

  1. 如果用户尝试访问 http://localhost:63145/Product/Index, ,他将被重定向到登录页面,并将控制返回给 产品控制器的 Index 方法,我没有创建任何会话。

所以在情况 2 中,不会创建用户会话。这里应该有什么更好的方法?

【问题讨论】:

  • 用户的会话没有创建是什么意思?您无法访问 Session 变量,例如 Session["SomeItem"] = "SomeValue" 吗?
  • 为什么不在登录 POST 方法中将值添加到 Session
  • 我的项目中没有登录页面,如上所述,它托管在不同的域@mihir,我的意思是,用于创建会话的代码在 HomeController 索引操作中

标签: asp.net-mvc asp.net-mvc-4 session asp.net-mvc-routing


【解决方案1】:

像这样创建BaseController

public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            InitAppController(filterContext);
            base.OnActionExecuting(filterContext);
        }

        // Redirect to login page if user's session is not valid.
        public void InitAppController(ActionExecutingContext filterContext)
        {
            if (/* Check Session Condition Here */)
            {
                filterContext.Result = RedirectToAction("Index", "Login");
            }
        }
    }

然后,更改所有控制器;

HomeController : ControllerHomeController : BaseController

ProductController : ControllerProductController : BaseController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多