C#后端处理session过期跳转登录页

发布时间:2022-06-20 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#后端处理session过期跳转登录页脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1,继承Controller并重写OnActionExecuting

protected override void OnActionExecuting(ActionExecutingContext filterContext) { //User does not exist,Test code //if (CurrentContext.CurrentUser == null) //{ // var currUser = new UserIdentity // { // Id = new Guid("40F7ACB1-5BBF-46CC-A47D-A42EBFDD470D"), // Name = "管理员", // IsAdmin = true // }; // HttpContext.Session.Add("CurrentUser", currUser); // return; //}

var theme = Request.QueryString["theme"];

if (!string.IsNullOrWhiteSpace(theme)) Session[BaseController.THEME] = theme; else if (theme != null) Session[BaseController.THEME] = null;

var actionName = RouteData.Values["action"].ToString(); var controllerName = RouteData.Values["controller"].ToString();

if (actionName != "ItemRedirectLink" || actionName != "ItemNoRedirectLink") { if (actionName == "Login" || actionName == "GetLoginQRCode" || actionName == "QueryWeChatLoginStatus" || actionName == "GetWXInfoThenLogin" || actionName == "SendToken" || actionName == "QueryReviewedResult" || actionName == "SendReviewAgain" || (!string.IsNullOrWhiteSpace(actionName) && actionName.Equals("About",StringComparison.OrdinalIgnoreCase))) { return; } if (actionName == "LoginWithNewAliExpress" || actionName == "SendToken") { return; } if (controllerName == "Saleplat") { if (actionName == "GetWishOrderInfoApiForJava" || actionName == "GetWishListingInfoApiForJava") { return; } }

var flg = WhetherAjaxRequest(filterContext);

var url = Request.Url.ToString();

if (CurrentContext.CurrentUser == null) { LoginOutHandle(filterContext, flg, 0); return; } }

base.OnActionExecuting(filterContext); }

protected bool WhetherAjaxRequest(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.Headers.Count > 0) { return IdhWebApplication.Extensions.Common.WhetherAjaxRequest(Request); }

return false; }

protected void LoginOutHandle(ActionExecutingContext filterContext, bool flg, int errorCode) { if (flg) { ErrorData errorData = new ErrorData(); errorData.status = Status.failure.ToString(); errorData.msg.errCode = errorCode;

JsonSerializerSettings jsSettings = new JsonSerializerSettings(); jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

var jsonData = JsonConvert.SerializeObject(errorData, jsSettings); Response.Write(jsonData); Response.End(); } else { var js = @"<script>if(window.top.location.protocol == 'file'){ location.href = '/Account/Login?fromUrl=' + encodeURIComponent(window.location.href) + '&errCode=" + errorCode + @"' } else{ location.href = '/Account/Login?fromUrl=' + encodeURIComponent(window.top.location.href) + '&errCode=" + errorCode + @"' }</script>";

Response.Write(js); Response.End(); }

filterContext.Result = new EmptyResult(); }

 

public class Common { public static bool WhetherAjaxRequest(HttpRequestBase request) { for (var i = 0; i < request.Headers.Count; i++) { var headerKey = request.Headers.GetKey(i);

if (headerKey.ToLower().Contains("accept")) { var headerValue = request.Headers.GetValues(i);

if (headerValue.Where(v => v.Contains("json")).Any()) { return true; } } }

return false; } }

脚本宝典总结

以上是脚本宝典为你收集整理的C#后端处理session过期跳转登录页全部内容,希望文章能够帮你解决C#后端处理session过期跳转登录页所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: