Browse Source

修正图片抓取上传的bug

懒得勤快 5 years ago
parent
commit
508d00bdb4

+ 3 - 3
src/Masuit.MyBlogs.Core/Controllers/ErrorController.cs

@@ -36,7 +36,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         public ActionResult Index()
         public ActionResult Index()
         {
         {
             Response.StatusCode = 404;
             Response.StatusCode = 404;
-            return Request.Method.ToLower().Equals("get") ? (ActionResult)View() : Json(new
+            return Request.Method.Equals(HttpMethods.Get) ? (ActionResult)View() : Json(new
             {
             {
                 StatusCode = 404,
                 StatusCode = 404,
                 Success = false,
                 Success = false,
@@ -77,7 +77,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                         break;
                         break;
                     case NotFoundException ex:
                     case NotFoundException ex:
                         Response.StatusCode = 404;
                         Response.StatusCode = 404;
-                        return Request.Method.ToLower().Equals("get") ? (ActionResult)View("Index") : Json(new
+                        return Request.Method.Equals(HttpMethods.Get) ? (ActionResult)View("Index") : Json(new
                         {
                         {
                             StatusCode = 404,
                             StatusCode = 404,
                             Success = false,
                             Success = false,
@@ -96,7 +96,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
             }
 
 
             Response.StatusCode = 503;
             Response.StatusCode = 503;
-            return Request.Method.ToLower().Equals("get") ? (ActionResult)View() : Json(new
+            return Request.Method.Equals(HttpMethods.Get) ? (ActionResult)View() : Json(new
             {
             {
                 StatusCode = 503,
                 StatusCode = 503,
                 Success = false,
                 Success = false,

+ 8 - 3
src/Masuit.MyBlogs.Core/Controllers/PassportController.cs

@@ -62,14 +62,17 @@ namespace Masuit.MyBlogs.Core.Controllers
                 from = HttpUtility.UrlDecode(from);
                 from = HttpUtility.UrlDecode(from);
                 Response.Cookies.Append("refer", from);
                 Response.Cookies.Append("refer", from);
             }
             }
+
             if (HttpContext.Session.Get<UserInfoDto>(SessionKey.UserInfo) != null)
             if (HttpContext.Session.Get<UserInfoDto>(SessionKey.UserInfo) != null)
             {
             {
                 if (string.IsNullOrEmpty(from))
                 if (string.IsNullOrEmpty(from))
                 {
                 {
                     return RedirectToAction("Index", "Home");
                     return RedirectToAction("Index", "Home");
                 }
                 }
-                return Redirect(from);
+
+                return LocalRedirect(from);
             }
             }
+
             if (Request.Cookies.Count > 2)
             if (Request.Cookies.Count > 2)
             {
             {
                 string name = Request.Cookies["username"];
                 string name = Request.Cookies["username"];
@@ -94,7 +97,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                         return RedirectToAction("Index", "Home");
                         return RedirectToAction("Index", "Home");
                     }
                     }
 
 
-                    return Redirect(from);
+                    return LocalRedirect(from);
                 }
                 }
             }
             }
 
 
@@ -117,11 +120,13 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
             {
                 return ResultData(null, false, "验证码错误");
                 return ResultData(null, false, "验证码错误");
             }
             }
+
             HttpContext.Session.Remove("valid"); //验证成功就销毁验证码Session,非常重要
             HttpContext.Session.Remove("valid"); //验证成功就销毁验证码Session,非常重要
             if (string.IsNullOrEmpty(username.Trim()) || string.IsNullOrEmpty(password.Trim()))
             if (string.IsNullOrEmpty(username.Trim()) || string.IsNullOrEmpty(password.Trim()))
             {
             {
                 return ResultData(null, false, "用户名或密码不能为空");
                 return ResultData(null, false, "用户名或密码不能为空");
             }
             }
+
             var userInfo = UserInfoService.Login(username, password);
             var userInfo = UserInfoService.Login(username, password);
             if (userInfo == null)
             if (userInfo == null)
             {
             {
@@ -200,7 +205,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             Response.Cookies.Delete("username");
             Response.Cookies.Delete("username");
             Response.Cookies.Delete("password");
             Response.Cookies.Delete("password");
             HttpContext.Session.Clear();
             HttpContext.Session.Clear();
-            return Request.Method.ToLower().Equals("get") ? RedirectToAction("Index", "Home") : ResultData(null, message: "注销成功!");
+            return Request.Method.Equals(HttpMethods.Get) ? RedirectToAction("Index", "Home") : ResultData(null, message: "注销成功!");
         }
         }
     }
     }
 }
 }

+ 6 - 5
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -1,10 +1,11 @@
-using Masuit.MyBlogs.Core.Common;
+using System;
+using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Configs;
 using Masuit.MyBlogs.Core.Configs;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Models;
 using Masuit.Tools.Models;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
 using Newtonsoft.Json;
-using System;
 using System.Net.Http;
 using System.Net.Http;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 
 
@@ -47,7 +48,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                     AddressDetail = ip.GetIPLocation() + "(本地数据库)"
                     AddressDetail = ip.GetIPLocation() + "(本地数据库)"
                 });
                 });
             }
             }
-            if (Request.Method.ToLower().Equals("get"))
+            if (Request.Method.Equals(HttpMethods.Get))
             {
             {
                 return View(address);
                 return View(address);
             }
             }
@@ -98,7 +99,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 if (address?.Status == 0)
                 if (address?.Status == 0)
                 {
                 {
                     ViewBag.Address = address.AddressResult.FormattedAddress;
                     ViewBag.Address = address.AddressResult.FormattedAddress;
-                    if (Request.Method.ToLower().Equals("get"))
+                    if (Request.Method.Equals(HttpMethods.Get))
                     {
                     {
                         return View(address.AddressResult.Location);
                         return View(address.AddressResult.Location);
                     }
                     }
@@ -110,7 +111,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.Address = addr;
             ViewBag.Address = addr;
             var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}");
             var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}");
             var physicsAddress = JsonConvert.DeserializeObject<PhysicsAddress>(s);
             var physicsAddress = JsonConvert.DeserializeObject<PhysicsAddress>(s);
-            if (Request.Method.ToLower().Equals("get"))
+            if (Request.Method.Equals(HttpMethods.Get))
             {
             {
                 return View(physicsAddress?.AddressResult?.Location);
                 return View(physicsAddress?.AddressResult?.Location);
             }
             }

+ 2 - 2
src/Masuit.MyBlogs.Core/Extensions/MyAuthorizeAttribute.cs

@@ -58,7 +58,7 @@ namespace Masuit.MyBlogs.Core.Extensions
                 }
                 }
                 else
                 else
                 {
                 {
-                    if (filterContext.HttpContext.Request.Method.ToLower().Equals("get"))
+                    if (filterContext.HttpContext.Request.Method.Equals(HttpMethods.Get))
                     {
                     {
                         filterContext.Result = new RedirectResult("/passport/login?from=" + HttpUtility.UrlEncode(filterContext.HttpContext.Request.Path.ToString())?.Replace("#", "%23"));
                         filterContext.Result = new RedirectResult("/passport/login?from=" + HttpUtility.UrlEncode(filterContext.HttpContext.Request.Path.ToString())?.Replace("#", "%23"));
                     }
                     }
@@ -70,7 +70,7 @@ namespace Masuit.MyBlogs.Core.Extensions
             }
             }
             else
             else
             {
             {
-                if (filterContext.HttpContext.Request.Method.ToLower().Equals("get"))
+                if (filterContext.HttpContext.Request.Method.Equals(HttpMethods.Get))
                 {
                 {
                     filterContext.Result = new RedirectResult("/passport/login?from=" + HttpUtility.UrlEncode(filterContext.HttpContext.Request.Path.ToString()));
                     filterContext.Result = new RedirectResult("/passport/login?from=" + HttpUtility.UrlEncode(filterContext.HttpContext.Request.Path.ToString()));
                 }
                 }

+ 4 - 4
src/Masuit.MyBlogs.Core/Extensions/UEditor/CrawlerHandler.cs

@@ -80,7 +80,7 @@ namespace Masuit.MyBlogs.Core.Extensions.UEditor
             }
             }
             try
             try
             {
             {
-                using var response = _httpClient.GetAsync(SourceUrl).Result;
+                using var response = await _httpClient.GetAsync(SourceUrl);
                 if (response.StatusCode != HttpStatusCode.OK)
                 if (response.StatusCode != HttpStatusCode.OK)
                 {
                 {
                     State = "Url returns " + response.StatusCode;
                     State = "Url returns " + response.StatusCode;
@@ -88,10 +88,10 @@ namespace Masuit.MyBlogs.Core.Extensions.UEditor
                 }
                 }
 
 
                 ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("catcherPathFormat"));
                 ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("catcherPathFormat"));
-                var stream = response.Content.ReadAsStreamAsync().Result;
-                var savePath = AppContext.BaseDirectory + "wwwroot" + ServerUrl;
+                var stream = await response.Content.ReadAsStreamAsync();
+                var savePath = Path.Combine(AppContext.BaseDirectory + "wwwroot", ServerUrl);
                 stream = stream.AddWatermark();
                 stream = stream.AddWatermark();
-                var (url, success) = Startup.ServiceProvider.GetRequiredService<ImagebedClient>().UploadImage(stream, savePath).Result;
+                var (url, success) = await Startup.ServiceProvider.GetRequiredService<ImagebedClient>().UploadImage(stream, savePath);
                 if (success)
                 if (success)
                 {
                 {
                     ServerUrl = url;
                     ServerUrl = url;

+ 1 - 2
src/Masuit.MyBlogs.Core/Extensions/UEditor/UploadHandler.cs

@@ -117,8 +117,7 @@ namespace Masuit.MyBlogs.Core.Extensions.UEditor
 
 
         private bool CheckFileType(string filename)
         private bool CheckFileType(string filename)
         {
         {
-            var fileExtension = Path.GetExtension(filename).ToLower();
-            return UploadConfig.AllowExtensions.Select(x => x.ToLower()).Contains(fileExtension);
+            return UploadConfig.AllowExtensions.Any(x => x.Equals(Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
         }
         }
 
 
         private bool CheckFileSize(long size)
         private bool CheckFileSize(long size)

+ 1 - 3
src/Masuit.MyBlogs.Core/Startup.cs

@@ -98,12 +98,11 @@ namespace Masuit.MyBlogs.Core
                 opt.UseMySql(AppConfig.ConnString, builder => builder.EnableRetryOnFailure(3)).EnableDetailedErrors().EnableSensitiveDataLogging();
                 opt.UseMySql(AppConfig.ConnString, builder => builder.EnableRetryOnFailure(3)).EnableDetailedErrors().EnableSensitiveDataLogging();
                 //opt.UseSqlServer(AppConfig.ConnString);
                 //opt.UseSqlServer(AppConfig.ConnString);
             }); //配置数据库
             }); //配置数据库
-            //services.AddCors(opt => opt.AddDefaultPolicy(p => p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); //配置跨域
             services.Configure<FormOptions>(options =>
             services.Configure<FormOptions>(options =>
             {
             {
                 options.MultipartBodyLengthLimit = 104857600; // 100MB
                 options.MultipartBodyLengthLimit = 104857600; // 100MB
             }); //配置请求长度
             }); //配置请求长度
-            services.AddSession(); //注入Session
+            services.AddSession().AddAntiforgery(); //注入Session
             services.AddWebSockets(opt => opt.ReceiveBufferSize = 4096 * 1024).AddSignalR().AddNewtonsoftJsonProtocol();
             services.AddWebSockets(opt => opt.ReceiveBufferSize = 4096 * 1024).AddSignalR().AddNewtonsoftJsonProtocol();
             services.AddHttpsRedirection(options =>
             services.AddHttpsRedirection(options =>
             {
             {
@@ -204,7 +203,6 @@ namespace Masuit.MyBlogs.Core
                     new MyRestrictiveAuthorizationFilter()
                     new MyRestrictiveAuthorizationFilter()
                 }
                 }
             }); //配置hangfire
             }); //配置hangfire
-            //app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); //配置跨域
             app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
             app.UseResponseCaching().UseResponseCompression(); //启动Response缓存
             app.UseRouting(); // 放在 UseStaticFiles 之后
             app.UseRouting(); // 放在 UseStaticFiles 之后
             app.UseEndpoints(endpoints =>
             app.UseEndpoints(endpoints =>