using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace zhengcaioa.Models
|
{
|
public class MyAttribute
|
{
|
|
}
|
public class CheckLoginAttribute : Attribute, IActionFilter
|
{
|
public bool IsCheck { get; set; } = true;
|
public void OnActionExecuted(ActionExecutedContext context)//方法执行后执行
|
{
|
|
}
|
|
public void OnActionExecuting(ActionExecutingContext context)//方法执行前执行
|
{
|
if (!IsCheck) return;
|
|
if (context.HttpContext.Session.GetString("User") == null)
|
{
|
// context.Result = new RedirectResult("/Home/Login"); //如果不存在这个Session则表示登陆不成功,跳到Contact页面
|
ContentResult Content = new ContentResult();
|
Content.Content = GetRestContent("登录过期,请重新登录");
|
Content.ContentType = "text/html";
|
|
context.Result = Content;
|
}
|
else
|
{
|
//否则,不做操作。
|
}
|
|
|
|
}
|
|
public static string GetRestContent(string msg)
|
{
|
StringBuilder jsContent = new StringBuilder();
|
jsContent.AppendLine("<script type=\"text/javascript\"> ");
|
jsContent.AppendLine(" if (typeof msg !== 'undefined') { ");
|
jsContent.AppendLine(" msg.tologin(\"" + msg + "\"); ");
|
jsContent.AppendLine(" } else if (typeof parent.msg !== 'undefined') { ");
|
jsContent.AppendLine(" parent.msg.tologin(\"" + msg + "\"); ");
|
jsContent.AppendLine(" } else { ");
|
jsContent.AppendLine(" alert(\"" + msg + "\"); ");
|
jsContent.AppendLine(" top.location.href = \"/home/login?m=\" + escape(\"" + msg + "\"); ");
|
jsContent.AppendLine(" } ");
|
jsContent.AppendLine("</script> ");
|
|
return jsContent.ToString();
|
}
|
}
|
}
|