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