Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace PBMS.Class
- {
- public class CustomAuthorize : AuthorizeAttribute
- {
- protected override bool AuthorizeCore(HttpContextBase httpContext)
- {
- if (httpContext.Session == null || httpContext.Session["UserLogin"] == null)
- return false;
- return base.AuthorizeCore(httpContext);
- }
- protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
- {
- string pathRef = "";
- var ajaxReq = filterContext.HttpContext.Request;
- try
- {
- pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
- }
- catch (Exception)
- {
- pathRef = "/Home/Index";
- }
- BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
- string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "Login",
- ReturnUrl = pathRef
- })).VirtualPath;
- if (ajaxReq.IsAjaxRequest())
- {
- //filterContext.Result = new JavaScriptResult() { Script = (@"jQueryEnforceDialog('#divOutOfSession', 'พบปัญหาในการเชื่อมต่อ กรุณา Login เข้าสู่ระบบใหม่อีกครั้ง', 'แจ้งเตือนการใช้งาน',function () { window.location='" + redirectPath + "';});") };
- filterContext.HttpContext.Response.Clear();
- filterContext.HttpContext.Response.StatusCode = 401;
- filterContext.HttpContext.Response.Flush();
- filterContext.HttpContext.Response.End();
- }
- else
- {
- filterContext.Result = new RedirectToRouteResult(
- new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "SessionUnauthorized",
- ReturnUrl = redirectPath
- })
- );
- }
- }
- }
- public class CustomActionFilter : ActionFilterAttribute, IActionFilter
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session["UserLogin"] == null)
- {
- string pathRef = "";
- var ajaxReq = filterContext.HttpContext.Request;
- try
- {
- pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
- }
- catch (Exception)
- {
- pathRef = "/Home/Index";
- }
- //BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
- string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "Login",
- ReturnUrl = pathRef
- })).VirtualPath;
- if (ajaxReq.IsAjaxRequest())
- {
- filterContext.Result = new HttpStatusCodeResult(401);
- }
- else
- {
- filterContext.Result = new RedirectToRouteResult(
- new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "SessionUnauthorized",
- ReturnUrl = redirectPath
- })
- );
- }
- //filterContext.Result = new EmptyResult();
- }
- else
- {
- base.OnActionExecuting(filterContext);
- }
- }
- public override void OnResultExecuting(ResultExecutingContext filterContext)
- {
- if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session["UserLogin"] == null)
- {
- string pathRef = "";
- var ajaxReq = filterContext.HttpContext.Request;
- try
- {
- pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
- }
- catch (Exception)
- {
- pathRef = "/Home/Index";
- }
- //BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
- string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "Login",
- ReturnUrl = pathRef
- })).VirtualPath;
- if (ajaxReq.IsAjaxRequest())
- {
- filterContext.Result = new HttpStatusCodeResult(401);
- }
- else
- {
- filterContext.Result = new RedirectToRouteResult(
- new RouteValueDictionary(
- new
- {
- controller = "Home",
- action = "SessionUnauthorized",
- ReturnUrl = redirectPath
- })
- );
- }
- //filterContext.Result = new EmptyResult();
- }
- else
- {
- base.OnResultExecuting(filterContext);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement