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;
}
}

posted @   元点  阅读(327)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示