Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7.  
  8. namespace PBMS.Class
  9. {
  10.     public class CustomAuthorize : AuthorizeAttribute
  11.     {
  12.         protected override bool AuthorizeCore(HttpContextBase httpContext)
  13.         {
  14.             if (httpContext.Session == null || httpContext.Session["UserLogin"] == null)
  15.                 return false;
  16.  
  17.             return base.AuthorizeCore(httpContext);
  18.         }
  19.  
  20.         protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
  21.         {
  22.             string pathRef = "";
  23.             var ajaxReq = filterContext.HttpContext.Request;
  24.             try
  25.             {
  26.                 pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
  27.             }
  28.             catch (Exception)
  29.             {
  30.                 pathRef = "/Home/Index";
  31.             }
  32.  
  33.             BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
  34.  
  35.             string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
  36.             new
  37.             {
  38.                 controller = "Home",
  39.                 action = "Login",
  40.                 ReturnUrl = pathRef
  41.             })).VirtualPath;
  42.  
  43.  
  44.             if (ajaxReq.IsAjaxRequest())
  45.             {
  46.                 //filterContext.Result = new JavaScriptResult() { Script = (@"jQueryEnforceDialog('#divOutOfSession', 'พบปัญหาในการเชื่อมต่อ กรุณา Login เข้าสู่ระบบใหม่อีกครั้ง', 'แจ้งเตือนการใช้งาน',function () { window.location='" + redirectPath + "';});") };
  47.                 filterContext.HttpContext.Response.Clear();
  48.                 filterContext.HttpContext.Response.StatusCode = 401;
  49.                 filterContext.HttpContext.Response.Flush();
  50.                 filterContext.HttpContext.Response.End();
  51.             }
  52.             else
  53.             {
  54.                 filterContext.Result = new RedirectToRouteResult(
  55.                         new RouteValueDictionary(
  56.                             new
  57.                             {
  58.                                 controller = "Home",
  59.                                 action = "SessionUnauthorized",
  60.                                 ReturnUrl = redirectPath
  61.                             })
  62.                         );
  63.             }
  64.         }
  65.     }
  66.  
  67.     public class CustomActionFilter : ActionFilterAttribute, IActionFilter
  68.     {
  69.         public override void OnActionExecuting(ActionExecutingContext filterContext)
  70.         {
  71.             if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session["UserLogin"] == null)
  72.             {
  73.                 string pathRef = "";
  74.                 var ajaxReq = filterContext.HttpContext.Request;
  75.                 try
  76.                 {
  77.                     pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
  78.                 }
  79.                 catch (Exception)
  80.                 {
  81.                     pathRef = "/Home/Index";
  82.                 }
  83.  
  84.                 //BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
  85.  
  86.                 string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
  87.                 new
  88.                 {
  89.                     controller = "Home",
  90.                     action = "Login",
  91.                     ReturnUrl = pathRef
  92.                 })).VirtualPath;
  93.  
  94.  
  95.                 if (ajaxReq.IsAjaxRequest())
  96.                 {
  97.                     filterContext.Result = new HttpStatusCodeResult(401);
  98.                 }
  99.                 else
  100.                 {
  101.                     filterContext.Result = new RedirectToRouteResult(
  102.                             new RouteValueDictionary(
  103.                                 new
  104.                                 {
  105.                                     controller = "Home",
  106.                                     action = "SessionUnauthorized",
  107.                                     ReturnUrl = redirectPath
  108.                                 })
  109.                             );
  110.                 }
  111.                 //filterContext.Result = new EmptyResult();
  112.             }
  113.             else
  114.             {
  115.                 base.OnActionExecuting(filterContext);
  116.             }
  117.         }
  118.  
  119.         public override void OnResultExecuting(ResultExecutingContext filterContext)
  120.         {
  121.             if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session["UserLogin"] == null)
  122.             {
  123.                 string pathRef = "";
  124.                 var ajaxReq = filterContext.HttpContext.Request;
  125.                 try
  126.                 {
  127.                     pathRef = filterContext.RequestContext.HttpContext.Request.RawUrl;
  128.                 }
  129.                 catch (Exception)
  130.                 {
  131.                     pathRef = "/Home/Index";
  132.                 }
  133.  
  134.                 //BMSLog.ErrorLog("Unauthorized - " + (filterContext.RequestContext.HttpContext.Request.UserHostAddress ?? ""), pathRef);
  135.  
  136.                 string redirectPath = RouteTable.Routes.GetVirtualPath(filterContext.RequestContext, new RouteValueDictionary(
  137.                 new
  138.                 {
  139.                     controller = "Home",
  140.                     action = "Login",
  141.                     ReturnUrl = pathRef
  142.                 })).VirtualPath;
  143.  
  144.  
  145.                 if (ajaxReq.IsAjaxRequest())
  146.                 {
  147.                     filterContext.Result = new HttpStatusCodeResult(401);
  148.                 }
  149.                 else
  150.                 {
  151.                     filterContext.Result = new RedirectToRouteResult(
  152.                             new RouteValueDictionary(
  153.                                 new
  154.                                 {
  155.                                     controller = "Home",
  156.                                     action = "SessionUnauthorized",
  157.                                     ReturnUrl = redirectPath
  158.                                 })
  159.                             );
  160.                 }
  161.                 //filterContext.Result = new EmptyResult();
  162.             }
  163.             else
  164.             {
  165.                 base.OnResultExecuting(filterContext);
  166.             }
  167.         }
  168.  
  169.        
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement