username@email.com
2025-04-27 15eb82df2d6ec539e9d4245bfe08d531e8eb6379
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
        }
    }
}