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 @ 2022-04-08 17:04  元点  阅读(392)  评论(0)    收藏  举报