Moli0
2021-12-15 24d68e7998e80cbd4ef3ed04acbf2539a9c5a46b
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
 
namespace Helpers
{
    //.net Framework MVCHelper
    //public class MVCHelper
    //{
    //    private HttpContextBase _HttpContext { get; set; }
    //    private HttpRequestBase _HttpRequest { get; set; }
    //    private HttpResponseBase _HttpResponse { get; set; }
 
    //    private MVCHelper() { }
 
    //    public MVCHelper(HttpContextBase httpContext) {
    //        _HttpContext = httpContext;
    //        _HttpRequest = _HttpContext.Request;
    //        _HttpResponse = _HttpContext.Response;
    //    }
 
    //    /// <summary>
    //    /// 设置本地cookie
    //    /// </summary>
    //    /// <param name="key">键</param>
    //    /// <param name="value">值</param>  
    //    /// <param name="minutes">过期时长,单位:分钟</param>      
    //    public void SetCookies(string key, string value, int minutes = 300,string path = "/",bool isDomain = false)
    //    {
    //        HttpCookie cookie = new HttpCookie(key, value);
    //        if (isDomain)
    //        {
    //            string host = Helpers.Tool.GetAppSetting("hostName");
    //            cookie.Domain = host;
    //        }
    //        cookie.Path = path;
    //        cookie.Expires = DateTime.Now.AddMinutes(minutes);
    //        _HttpResponse.Cookies.Add(cookie);
    //    }
 
    //    /// <summary>
    //    /// 设置只读cookie
    //    /// </summary>
    //    /// <param name="key">键</param>
    //    /// <param name="value">值</param>  
    //    /// <param name="minutes">过期时长,单位:分钟</param>  
    //    public void SetOnlyCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
    //    {
    //        HttpCookie cookie = new HttpCookie(key, value);
    //        if (isDomain)
    //        {
    //            string host = Helpers.Tool.GetAppSetting("hostName");
    //            cookie.Domain = host;
    //        }
    //        cookie.Path = path;
    //        cookie.HttpOnly = true;
    //        cookie.Expires = DateTime.Now.AddMinutes(minutes);
    //        _HttpRequest.Cookies.Add(cookie);
    //    }
 
 
    //    /// <summary>
    //    /// 删除指定的cookie
    //    /// </summary>
    //    /// <param name="key">键</param>
    //    public void DeleteCookies(string key)
    //    {
    //        HttpCookie cookie = _HttpRequest.Cookies[key];
    //        if (cookie == null) {
    //            return;
    //        }
    //        cookie.Expires = DateTime.Now.AddDays(-1);
    //        _HttpResponse.AppendCookie(cookie);
    //    }
 
    //    /// <summary>
    //    /// 更新cookie
    //    /// </summary>
    //    /// <param name="key">键</param>
    //    /// <param name="value">值</param>  
    //    /// <param name="minutes">过期时长,单位:分钟</param>  
    //    public void UpdateCookies(string key, string value, int minutes = 300, string path = "/") {
    //        HttpCookie cookie = _HttpRequest.Cookies[key];
    //        if (cookie == null)
    //        {
    //            return;
    //        }
    //        cookie.Path = path;
    //        cookie.Value = value;
    //        cookie.Expires = DateTime.Now.AddMinutes(minutes);
    //        _HttpResponse.AppendCookie(cookie);
    //    }
 
    //    /// <summary>
    //    /// 获取cookies
    //    /// </summary>
    //    /// <param name="key">键</param>
    //    /// <returns>返回对应的值</returns>
    //    public string GetCookies(string key)
    //    {
    //        HttpCookie cookie = _HttpRequest.Cookies.Get(key);
    //        if (cookie == null)
    //        {
    //            return null;
    //        }
    //        string value = cookie.Value;
    //        if (string.IsNullOrEmpty(value))
    //            value = string.Empty;
    //        return value;
    //    }
 
 
    //}
 
    public class CoreMVCHelper
    {
        private HttpContext _HttpContext { get; set; }
        private HttpRequest _HttpRequest { get; set; }
        private HttpResponse _HttpResponse { get; set; }
 
        private CoreMVCHelper() { }
 
        public CoreMVCHelper(HttpContext httpContext)
        {
            _HttpContext = httpContext;
            _HttpRequest = _HttpContext.Request;
            _HttpResponse = _HttpContext.Response;
        }
 
        /// <summary>
        /// 设置本地cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>  
        /// <param name="minutes">过期时长,单位:分钟</param>      
        public void SetCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
        {
            CookieOptions cookieOptions = new CookieOptions();
            if (isDomain) {
                string host = Helpers.Tool.GetSystemConfig().HostName;
                cookieOptions.Domain = host;
            }
            cookieOptions.Path = path;
            cookieOptions.Expires = DateTime.Now.AddMinutes(minutes);
 
            _HttpContext.Response.Cookies.Append(key, value, cookieOptions);
 
        }
 
        /// <summary>
        /// 设置只读cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>  
        /// <param name="minutes">过期时长,单位:分钟</param>  
        public void SetOnlyCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
        {
            CookieOptions cookieOptions = new CookieOptions();
            if (isDomain)
            {
                string host = Helpers.Tool.GetSystemConfig().HostName;
                cookieOptions.Domain = host;
            }
            cookieOptions.Path = path;
            cookieOptions.Expires = DateTime.Now.AddMinutes(minutes);
            cookieOptions.HttpOnly = true;
 
            _HttpContext.Response.Cookies.Append(key, value, cookieOptions);
        }
 
 
        /// <summary>
        /// 删除指定的cookie
        /// </summary>
        /// <param name="key">键</param>
        public void DeleteCookies(string key)
        {
            _HttpContext.Response.Cookies.Delete(key);
        }
 
        /// <summary>
        /// 更新cookie
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>  
        /// <param name="minutes">过期时长,单位:分钟</param>  
        public void UpdateCookies(string key, string value, int minutes = 300, string path = "/")
        {
            SetCookies(key, value, minutes, path);
        }
 
        /// <summary>
        /// 获取cookies
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>返回对应的值</returns>
        public string GetCookies(string key)
        {
            string value = string.Empty;
            if (_HttpContext.Request.Cookies.TryGetValue(key, out value)) {
                return value;
            }
            else {
                return string.Empty;
            }
        }
 
 
    }
 
 
    public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            if (context.Exception != null)
            {
                //LogSystemHelper.Error_Log.Write(context.Exception, MethodBase.GetCurrentMethod(), "程序运行异常");
                Helpers.Tool.PrintLog_Error("程序运行异常" + context.Exception.Message, context.Exception.StackTrace);
                context.HttpContext.Response.StatusCode = 500;
                context.Result = new Microsoft.AspNetCore.Mvc.JsonResult(new { status = "error", code = 1, msg = "程序运行异常" });
            }
        }
    }
}