xuruiqian
2025-06-04 5955ac5715a811407e183042875fd1d130572d58
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using GTech.Solution.Api.Common.Common;
 
namespace GTech.Solution.Api
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
           
        }
 
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            var request = context.Request;
            var response = context.Response;
 
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
 
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*");
                HttpContext.Current.Response.AddHeader("Access-Control-Request-Headers", "*");
 
 
                HttpContext.Current.Response.End();
            }
 
            CheckAccessPermission(request, response);
        }
 
        private void CheckAccessPermission(HttpRequest request, HttpResponse response)
        {
            try
            {
                var requestHeader = request.Headers;
                var requestUrl = request.Url;
                var token = request.Headers["Authorization"];
 
                var isNeedValidateJwtToken = true;
 
                if (requestUrl.AbsolutePath == "/")
                {
                    isNeedValidateJwtToken = false;
                }
                else
                {
                    var IgnoringAntPathRequestMatcher = new List<string>();
                    var ignoringAntPathRequestMatcherStr = ConfigurationManager.AppSettings["IgnoringAntPathRequestMatcher"];
 
                    if (!string.IsNullOrEmpty(ignoringAntPathRequestMatcherStr))
                    {
                        var ignoringAntPathRequestMatcherArray = ignoringAntPathRequestMatcherStr.Split(',');
 
                        IgnoringAntPathRequestMatcher.AddRange(ignoringAntPathRequestMatcherArray);
                    }
 
                    IgnoringAntPathRequestMatcher.ForEach(en =>
                    {
                        if (requestUrl.AbsolutePath.Contains(en))
                        {
                            isNeedValidateJwtToken = false;
                        }
                    });
                }
 
                if (isNeedValidateJwtToken)
                {
                    TokenGenerator.ValidateJwtToken(token);
 
                    var userbasic = TokenGenerator.GetUserbasic(token);
                    if (userbasic != null)
                    {
                        //Check permissions
                    }
                }
            }
            catch (Exception ex)
            {
                response.ContentType = "application/json";
                response.Write(ex.Message);
                response.End();
            }
        }
 
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            
        }
 
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            try
            {
 
            }
            catch { }
        }
 
        protected void Application_End(object sender, EventArgs e)
        {
            try
            {
 
            }
            catch { }
        }
 
        private ResponseCompressionType GetCompressionMode(HttpRequest request)
        {
            string acceptEncoding = request.Headers["Accept-Encoding"];
            if (string.IsNullOrEmpty(acceptEncoding))
            {
                return ResponseCompressionType.None;
            }
            acceptEncoding = acceptEncoding.ToUpperInvariant();
            if (acceptEncoding.Contains("GZIP"))
            {
                return ResponseCompressionType.GZip;
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                return ResponseCompressionType.Deflate;
            }
            else
            {
                return ResponseCompressionType.None;
            }
        }
 
        private enum ResponseCompressionType { None, GZip, Deflate }
 
    }
}