From 314c9c0d1fb85c66a2a9998ce1cbfc572c029188 Mon Sep 17 00:00:00 2001
From: lingyang <369845068@qq.com>
Date: Fri, 30 Jun 2023 14:12:12 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B7=A8=E5=9F=9F=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
24Hour/Controllers/LoginController.cs | 14 +++++++--
.../Model/CustomCorsActionFilterAttribute.cs | 18 +++++++++++
24Hour/Program.cs | 30 ++++++++++++-------
3 files changed, 50 insertions(+), 12 deletions(-)
create mode 100644 24Hour/Model/CustomCorsActionFilterAttribute.cs
diff --git a/24Hour/Controllers/LoginController.cs b/24Hour/Controllers/LoginController.cs
index e9a08a1..4b6d7f9 100644
--- a/24Hour/Controllers/LoginController.cs
+++ b/24Hour/Controllers/LoginController.cs
@@ -1,3 +1,4 @@
+using _24Hour.Model;
using com.sun.xml.@internal.ws.api.model;
using Elight.Entity;
using Elight.Logic;
@@ -47,11 +48,19 @@ namespace _24Hour.Controllers
///
[HttpPost]
[Route("SystemLogin")]
+ [CustomCorsActionFilterAttribute]
public async Task SystemLogin(UserLogin login)
{
try
{
- var Passmd5 = Md5.Encrypt32(login.Password).ToLower();
+ var Passmd5 = Md5.Encrypt32(login.Password).ToLower();
+ var model = await _db.Queryable().Where(x => x.phone== login.phone && x.IsDeleted==0).FirstAsync();
+ if (model == null)
+ {
+ ret.IsSucceed = false;
+ ret.Message = "账号不存在,请先注册!";
+ return ret;
+ }
var date = await _db.Queryable().Where(q => q.IsDeleted == 0&&q.usertype==0 && q.isdeactivate == 0 && q.phone == login.phone&&q.Password== Passmd5).FirstAsync();
if (date != null)
{
@@ -137,6 +146,7 @@ namespace _24Hour.Controllers
///
[HttpPost]
[Route("Login")]
+ [CustomCorsActionFilterAttribute]
public async Task Login(UserLogin login)
{
try
@@ -155,7 +165,7 @@ namespace _24Hour.Controllers
if (Passmd5 != date.Password)
{
ret.IsSucceed = false;
- ret.Message = "账号或者密码错误!";
+ ret.Message = "账号或密码错误!";
return ret;
}
//else if (date.usertype == 1 && date.audit == null)
diff --git a/24Hour/Model/CustomCorsActionFilterAttribute.cs b/24Hour/Model/CustomCorsActionFilterAttribute.cs
new file mode 100644
index 0000000..8414ea5
--- /dev/null
+++ b/24Hour/Model/CustomCorsActionFilterAttribute.cs
@@ -0,0 +1,18 @@
+锘縰sing Microsoft.AspNetCore.Mvc.Filters;
+
+namespace _24Hour.Model
+{
+ public class CustomCorsActionFilterAttribute : Attribute, IActionFilter
+ {
+
+ public void OnActionExecuting(ActionExecutingContext context)
+ {
+ context.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
+ }
+
+ public void OnActionExecuted(ActionExecutedContext context)
+ {
+
+ }
+ }
+}
diff --git a/24Hour/Program.cs b/24Hour/Program.cs
index c762e10..6a24efa 100644
--- a/24Hour/Program.cs
+++ b/24Hour/Program.cs
@@ -75,16 +75,24 @@ builder.Services.AddSwaggerGen(c =>
});
#endregion
// 配置跨域
-//builder.Services.AddCors(options => options.AddPolicy("CorsPolicy",
-//c =>
-//{
-// c.WithOrigins(Configuration.GetSection("UrlsConfiguration:CorUrls").Value.Split(',', StringSplitOptions.RemoveEmptyEntries))
-// .AllowAnyHeader() // 允许任何标头(这个最好写到AllowAnyMethod上面去)
-// .AllowAnyMethod() // 允许任何方法访问
-// .SetIsOriginAllowed(o => true) // =AllowAnyOrigin()
-// .AllowCredentials();
-//}));
-//清除jwt toekn
+builder.Services.AddCors(policy =>
+{
+ policy.AddPolicy("CorsPolicy", opt => opt
+ .AllowAnyOrigin()
+ .AllowAnyHeader()
+ .AllowAnyMethod()
+ .WithExposedHeaders("X-Pagination"));
+});
+//builder.Services.AddCors(options => options.AddPolicy("CorsPolicy",
+//c =>
+//{
+// c.WithOrigins(Configuration.GetSection("UrlsConfiguration:CorUrls").Value.Split(',', StringSplitOptions.RemoveEmptyEntries))
+// .AllowAnyHeader() // 允许任何标头(这个最好写到AllowAnyMethod上面去)
+// .AllowAnyMethod() // 允许任何方法访问
+// .SetIsOriginAllowed(o => true) // =AllowAnyOrigin()
+// .AllowCredentials();
+//}));
+//清除jwt toekn
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
// 添加身份验证服务
@@ -226,6 +234,8 @@ app.UseSwaggerUI(c =>
c.RoutePrefix = string.Empty;
});
#endregion
+
+app.UseCors("CorsPolicy");
app.Run();
#endregion