songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace BatchService.Framework.Utility
{
    /// <summary>
    /// Cookie帮助类
    /// </summary>
    public class Cookie
    {
        /// <summary>
        /// 取Cookie
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static HttpCookie Get(string name)
        {
            return HttpContext.Current.Request.Cookies[name];
        }
 
        /// <summary>
        /// 取Cookie值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetValue(string name)
        {
            var httpCookie = Get(name);
            if (httpCookie != null)
                return httpCookie.Value;
            else 
                return string.Empty;
        }
 
        /// <summary>
        /// 移除Cookie
        /// </summary>
        /// <param name="name"></param>
        public static void Remove(string name)
        {
            Cookie.Remove(Cookie.Get(name));
        }
 
        public static void Remove(HttpCookie cookie)
        {
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now;
                Cookie.Save(cookie);
            }
        }
 
        /// <summary>
        /// 保存Cookie
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="expiresHours"></param>
        public static void Save(string name, string value, int expiresHours = 0)
        {
            var httpCookie = Get(name);
            if (httpCookie == null)
                httpCookie = Set(name);
 
            httpCookie.Value = value;
            Cookie.Save(httpCookie, expiresHours);
        }
 
 
        public static void Save(HttpCookie cookie, int expiresHours = 0)
        {
            //string domain = Fetch.ServerDomain;
            //string urlHost = HttpContext.Current.Request.Url.Host.ToLower();
            //if (domain != urlHost)
            //    cookie.Domain = domain;
 
            //if (expiresHours > 0)
            //    cookie.Expires = DateTime.Now.AddMinutes(expiresHours);
 
            //HttpContext.Current.Response.Cookies.Add(cookie);
            Save(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, cookie, expiresHours * 60);
        }
 
        public static void Save(HttpContextBase context, HttpCookie cookie, int expiresMinutes = 0)
        {
            string domain = Fetch.ServerDomain;
            string urlHost = context.Request.Url.Host.ToLower();
            if (domain != urlHost)
                cookie.Domain = domain;
 
            if (expiresMinutes > 0)
                cookie.Expires = DateTime.Now.AddMinutes(expiresMinutes);
            
            context.Response.Cookies.Add(cookie);
        }
 
        public static HttpCookie Set(string name)
        {
            return new HttpCookie(name);
        }
 
        private static Dictionary<string, string> sessionIdMap = new Dictionary<string, string>();
        private static Dictionary<string, string> loginFailsMap = new Dictionary<string, string>();
       
         
        public static bool isValidUser(string id, bool isLogin=false)
        {
            string sessionId = HttpContext.Current.Session.SessionID;
            if (isLogin)
            { 
                sessionIdMap.Add(sessionId, id);
                return true;
            }
            string historySessionId = sessionIdMap.ContainsKey(sessionId) ? sessionIdMap[sessionId] : "";
            if (id!=historySessionId)
            { 
                return false;
            }
            return true;
        }
        
    }
}