Selaa lähdekoodia

OptionsSetting获取nacos实时数据调整

lichunlei 2 päivää sitten
vanhempi
commit
b0c9dbe1ae
2 muutettua tiedostoa jossa 48 lisäystä ja 45 poistoa
  1. 1 1
      App/App.cs
  2. 47 44
      Filters/DecryptMiddleware.cs

+ 1 - 1
App/App.cs

@@ -9,7 +9,7 @@ namespace Infrastructure
         /// <summary>
         /// 全局配置文件
         /// </summary>
-        public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService<IOptions<OptionsSetting>>()?.Value);
+        public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService<IOptionsMonitor<OptionsSetting>>()?.CurrentValue);
 
         /// <summary>
         /// 服务提供器

+ 47 - 44
Filters/DecryptMiddleware.cs

@@ -13,54 +13,57 @@ namespace Filters
             string cipherText = "";
             try
             {
-                if(request.Method.ToLower() == "get")
+                if(!request.Path.Value.Contains("upload"))
                 {
-                    cipherText = request.Query["value"];
-                    cipherText = HttpUtility.UrlDecode(cipherText);
-                    cipherText = Decrypt(cipherText);
-                    cipherText = HttpUtility.UrlDecode(cipherText);
-                }
-                else if(request.Method.ToLower() == "delete")
-                {
-                    string path = request.Path.Value;
-                    string value = path.Substring(path.LastIndexOf("/") + 1);
-                    path = path.Substring(0, path.LastIndexOf("/") + 1);
-                    value = Decrypt(value);
-                    path += value;
-                    request.Path = new PathString(path);
-                    request.RouteValues["id"] = value;
-                }
-                else
-                {
-                    using var reader = new StreamReader(request.Body, leaveOpen: true);
-                    cipherText = reader.ReadToEnd();
-                    if(cipherText.StartsWith("value=")) 
+                    if(request.Method.ToLower() == "get")
                     {
-                        cipherText = cipherText.Replace("value=", "");
+                        cipherText = request.Query["value"];
+                        cipherText = HttpUtility.UrlDecode(cipherText);
+                        cipherText = Decrypt(cipherText);
+                        cipherText = HttpUtility.UrlDecode(cipherText);
+                    }
+                    else if(request.Method.ToLower() == "delete")
+                    {
+                        string path = request.Path.Value;
+                        string value = path.Substring(path.LastIndexOf("/") + 1);
+                        path = path.Substring(0, path.LastIndexOf("/") + 1);
+                        value = Decrypt(value);
+                        path += value;
+                        request.Path = new PathString(path);
+                        request.RouteValues["id"] = value;
+                    }
+                    else
+                    {
+                        using var reader = new StreamReader(request.Body, leaveOpen: true);
+                        cipherText = reader.ReadToEnd();
+                        if(cipherText.StartsWith("value=")) 
+                        {
+                            cipherText = cipherText.Replace("value=", "");
+                        }
+                        cipherText = Decrypt(cipherText);
+                    }
+                    if (string.IsNullOrWhiteSpace(cipherText))
+                    {
+                        return 1;
                     }
-                    cipherText = Decrypt(cipherText);
-                }
-                if (string.IsNullOrWhiteSpace(cipherText))
-                {
-                    return 1;
-                }
 
-                if(request.Method.ToLower() == "get")
-                {
-                    cipherText = GetParams(cipherText);
-                    var queryDict = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(cipherText);
-                    var newQuery = new QueryCollection(queryDict);
-                    request.Query = newQuery;
-                    request.QueryString = new QueryString("?" + cipherText);
-                }
-                else if(request.Method.ToLower() == "delete") {}
-                else
-                {
-                    var bytes = Encoding.UTF8.GetBytes(cipherText);
-                    var ms = new MemoryStream(bytes);
-                    request.Body = ms;        // 替换为明文
-                    request.ContentLength = bytes.Length;
-                    ms.Position = 0;
+                    if(request.Method.ToLower() == "get")
+                    {
+                        cipherText = GetParams(cipherText);
+                        var queryDict = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(cipherText);
+                        var newQuery = new QueryCollection(queryDict);
+                        request.Query = newQuery;
+                        request.QueryString = new QueryString("?" + cipherText);
+                    }
+                    else if(request.Method.ToLower() == "delete") {}
+                    else
+                    {
+                        var bytes = Encoding.UTF8.GetBytes(cipherText);
+                        var ms = new MemoryStream(bytes);
+                        request.Body = ms;        // 替换为明文
+                        request.ContentLength = bytes.Length;
+                        ms.Position = 0;
+                    }
                 }
             }
             catch (Exception ex)