当前位置: 代码迷 >> ASP.NET >> mvc 截取请求之后如何重定向,或者修改页面url
  详细解决方案

mvc 截取请求之后如何重定向,或者修改页面url

热度:3584   发布时间:2013-02-25 00:00:00.0
mvc 截取请求之后怎么重定向,或者修改页面url
问题描述:
我登陆进系统主页,然后再打开一个新页面,把系统地址再复制进去(效果:判断cookie直接进去主页)
这时我随便把一个页面注销(清除cookies)并且跳到登录页,然后切换到没点注销的那个页,里面权限操作都可以点,这时错误一大堆了,然后我想要的是里面任何请求,都要进行判断,然后我就用了IHttpModule 截取是截取到了,但是我把地址重定向,变成我请求的地址(比如后台的Index/xml_doc 的方法),变成请求context.Response.Redirect("/");//这个代表跳到登录页 
我页面上有个 弹出曾 单击弹出div 里面是iframe ,然后弹出的iframe的地址变成 主页的地址,- -!,我要的是整个页面重定向到 登录页啊。而不是请求页改为登录页。


 

C# code
 private void Application_BeginRequest(Object source,         EventArgs e)    {             // Create HttpApplication and HttpContext objects to access        // request and response properties.        HttpApplication application = (HttpApplication)source;        HttpContext context = application.Context;      //  string s = context.Request.Url.ToString();               string filePath = context.Request.FilePath;        string fileExtension =            VirtualPathUtility.GetExtension(filePath);        string path = Regex.Match(filePath, @"[Index]+").ToString();        if (path == "Index")        {            if (Common.IsLogin() == false)  //判断用户是否登陆   ///大侠们看这边!!!!!            {                context.Response.End();                                  context.Response.Redirect("/");                            }                    }              //if (fileExtension.Equals("/Index/Index"))        //{        // //   string s = context.Request.Url.AbsolutePath.ToString();        //    context.Response.Write("<h1><font color=red>" +        //        "VerificationModule: Beginning of Request" +        //        "</font></h1><hr>");        //}    }


------解决方案--------------------------------------------------------
在Controller上面加[Authorize]特性,然后把配置文件里的<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>指向你的登录页
------解决方案--------------------------------------------------------
asp.net mvc里应该用filter完成这个工作,不要写在Application_BeginRequest里
------解决方案--------------------------------------------------------
探讨

引用:

在Controller上面加[Authorize]特性,然后把配置文件里的<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>指向你的登录页


这个是一旦访问这个 加特性的Controller 会跳转到 登录页……

------解决方案--------------------------------------------------------
C# code
protected override bool AuthorizeCore(HttpContextBase httpContext)        {            string url = httpContext.Request.RawUrl;            if (url.Equals("/") || url.Contains("/Home/LoginOn/") || url.Contains("/Home/CheckCode"))            {                return true;            }            if (CC6UU_Admin.Commom.AdminInfo.Session == null)            {                httpContext.Response.StatusCode = 404;                return false;            }            else            {                return true;            }        }        public override void OnAuthorization(AuthorizationContext filterContext)        {            base.OnAuthorization(filterContext);            if (filterContext.HttpContext.Response.StatusCode == 404)            {                filterContext.Result = new RedirectResult("/");            }        }
  相关解决方案