diff --git a/projects/Infrastructure/Extensions/HttpContextExtensions.cs b/projects/Infrastructure/Extensions/HttpContextExtensions.cs index 1b613a9a..7396e105 100644 --- a/projects/Infrastructure/Extensions/HttpContextExtensions.cs +++ b/projects/Infrastructure/Extensions/HttpContextExtensions.cs @@ -1,12 +1,10 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; -using System.Linq; +using System.Reflection; using System.Security.Claims; using System.Text; @@ -14,7 +12,12 @@ namespace Infrastructure.Extensions { public static class HttpContextExtensions { - public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg) + public static string GetJwtCookieName(this HttpContext httpContext) + { + return $"jwt-{Assembly.GetEntryAssembly().GetName().Name.ToLower()}"; + } + + public static void JwtSignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg) { var token = httpContext.GetToken(userName, cfg, DateTime.Now.AddYears(1)); var cookieOptions = new CookieOptions @@ -25,8 +28,14 @@ namespace Infrastructure.Extensions { cookieOptions.Expires = DateTimeOffset.Now.AddYears(1); } - httpContext.Response.Cookies.Delete("jwt"); - httpContext.Response.Cookies.Append("jwt", token, cookieOptions); + var cookieName = httpContext.GetJwtCookieName(); + httpContext.Response.Cookies.Delete(cookieName); + httpContext.Response.Cookies.Append(cookieName, token, cookieOptions); + } + + public static void JwtSignOut(this HttpContext httpContext) + { + httpContext.Response.Cookies.Delete(httpContext.GetJwtCookieName()); } public static string GetToken(this HttpContext httpContext, string userName, IConfiguration cfg, DateTime expires) diff --git a/projects/Infrastructure/Web/BaseStartup.cs b/projects/Infrastructure/Web/BaseStartup.cs index 74e9facf..c074c562 100644 --- a/projects/Infrastructure/Web/BaseStartup.cs +++ b/projects/Infrastructure/Web/BaseStartup.cs @@ -267,7 +267,7 @@ namespace Infrastructure.Web { if (!context.Request.IsAjax()) { - context.Response.Redirect("Account/Login"); + context.Response.Redirect("/Account/Login"); context.HandleResponse(); } return Task.CompletedTask; @@ -281,9 +281,10 @@ namespace Infrastructure.Web { context.Token = context.Request.Query["access_token"]; } - if (!context.Request.Headers.ContainsKey("Authorization") && context.Request.Cookies.Keys.Contains("jwt")) + var jwtCookieName = context.HttpContext.GetJwtCookieName(); + if (!context.Request.Headers.ContainsKey("Authorization") && context.Request.Cookies.Keys.Contains(jwtCookieName)) { - context.Token = context.Request.Cookies["jwt"]; + context.Token = context.Request.Cookies[jwtCookieName]; } } return Task.CompletedTask; diff --git a/projects/IoT.Shared/Application/Domain/Entities/Users/User.cs b/projects/IoT.Shared/Application/Domain/Entities/Users/User.cs index b2d39960..64eb1b0b 100644 --- a/projects/IoT.Shared/Application/Domain/Entities/Users/User.cs +++ b/projects/IoT.Shared/Application/Domain/Entities/Users/User.cs @@ -7,26 +7,12 @@ namespace Application.Domain.Entities [Display(Name = "用户")] public class User : BaseEntity { - /// - /// 登录名 - /// public string UserName { get; set; } - - /// - /// 加密混淆随机数 - /// + public string NickName { get; set; } + public string Avatar { get; set; } public string SecurityStamp { get; set; } - - /// - /// 加密密码 - /// public string PasswordHash { get; set; } - - /// - /// 邮箱 - /// public string Email { get; set; } - public List UserRoles { get; set; } = new List(); } } \ No newline at end of file diff --git a/projects/IoT.Shared/IoTServiceStartup.cs b/projects/IoT.Shared/IoTServiceStartup.cs index 165ce0f9..e943ca57 100644 --- a/projects/IoT.Shared/IoTServiceStartup.cs +++ b/projects/IoT.Shared/IoTServiceStartup.cs @@ -1,20 +1,15 @@ using Application.Domain.Entities; -using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Security; using Infrastructure.UI; using Infrastructure.Web; using IoT.Shared.Services; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace IoT.UI.Shard { diff --git a/projects/IoTCenter/Controllers/AccountController.cs b/projects/IoTCenter/Controllers/AccountController.cs index 68636767..14371160 100644 --- a/projects/IoTCenter/Controllers/AccountController.cs +++ b/projects/IoTCenter/Controllers/AccountController.cs @@ -19,21 +19,21 @@ namespace IoTCenter.Controllers [Authorize] public class AccountController : BaseController { - private readonly IConfiguration _configuration; + private readonly IConfiguration _cfg; private readonly IRepository _userRepo; private readonly IStringLocalizer _localizer; private readonly IEncryptionService _encryptionService; private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; - public AccountController(IConfiguration configuration, + public AccountController(IConfiguration cfg, IRepository userRepo, IEncryptionService encryptionService, IStringLocalizer localizer, IEmailSender emaliSender, ISmsSender smsSender) { - this._configuration = configuration; + this._cfg = cfg; this._userRepo = userRepo; this._encryptionService = encryptionService; this._localizer = localizer; @@ -59,20 +59,51 @@ namespace IoTCenter.Controllers public IActionResult Logout() { var fullReturnUrl = Url.FullAction("Index", "Home"); - var logoutUrl = this._configuration["usercenter:logout"]; + var logoutUrl = this._cfg["usercenter:logout"]; var url = logoutUrl.SetParam("returnUrl", fullReturnUrl); return Redirect(url); } + [AllowAnonymous] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] + public string JsonpLogin(string userName, string nickName, string avatar, string timestamp, bool rememberMe, string sign) + { + try + { + var key = this._cfg.GetSection("usercenter").GetValue("key"); + if (string.Concat(userName, timestamp, key).Md5() == sign) + { + var user = this._userRepo.ReadOnlyTable().FirstOrDefault(o => o.UserName == userName); + if (user == null) + { + user = new User { UserName = userName, NickName = nickName, Avatar = avatar }; + this._userRepo.Add(user); + this._userRepo.SaveChanges(); + } + HttpContext.JwtSignIn(userName, rememberMe, _cfg); + return ""; + } + else + { + return $"function(){{return \"{userName} login error\";}}();"; + } + } + catch (Exception ex) + { + ex.PrintStack(); + return $"function(){{return {ex.Message};}}();"; + } + } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] public string JsonpLogout(string userName, string timestamp, string sign) { try { - var key = this._configuration["usercenter:key"]; + var key = this._cfg["usercenter:key"]; if (string.Concat(userName, timestamp, key).Md5() == sign) { - HttpContext.SignOutAsync(); + HttpContext.JwtSignOut(); return ""; } else @@ -92,7 +123,7 @@ namespace IoTCenter.Controllers public IActionResult Login(string returnUrl = null) { var fullReturnUrl = Url.GetFullUrl(returnUrl ?? "~"); - var loginUrl = this._configuration["usercenter:login"]; + var loginUrl = this._cfg["usercenter:login"]; var url = loginUrl.SetParam(nameof(returnUrl), fullReturnUrl); return Redirect(url); } @@ -136,7 +167,7 @@ namespace IoTCenter.Controllers public IActionResult Register() { var fullReturnUrl = Url.FullAction("Index", "Home"); - var registerUrl = this._configuration["usercenter:register"]; + var registerUrl = this._cfg["usercenter:register"]; var url = registerUrl.SetParam("returnUrl", fullReturnUrl); return Redirect(url); } diff --git a/projects/IoTCenter/Views/Home/Node.cshtml b/projects/IoTCenter/Views/Home/Node.cshtml index 0a55eed4..19f31b27 100644 --- a/projects/IoTCenter/Views/Home/Node.cshtml +++ b/projects/IoTCenter/Views/Home/Node.cshtml @@ -171,13 +171,13 @@
-

- {{GetDeviceDataAttr(device.number,'温度','Description')}} +

+ {{GetDeviceDataAttr(device.number,'温度','description')}}

- 温度:{{GetDeviceDataAttr(device.number,'温度','Value')}}{{ GetDeviceDataAttr(device.number,'温度','Unit')}} + 温度:{{GetDeviceDataAttr(device.number,'温度','value')}}{{ GetDeviceDataAttr(device.number,'温度','unit')}}
@@ -188,13 +188,13 @@
-

- {{GetDeviceDataAttr(device.number,'湿度','Description')}} +

+ {{GetDeviceDataAttr(device.number,'湿度','description')}}

- 湿度:{{GetDeviceDataAttr(device.number,'湿度','Value')}}{{ GetDeviceDataAttr(device.number,'湿度','Unit')}} + 湿度:{{GetDeviceDataAttr(device.number,'湿度','value')}}{{ GetDeviceDataAttr(device.number,'湿度','unit')}}
@@ -208,13 +208,13 @@
-

- {{GetDeviceDataAttr(device.number,'PM2.5','Description')}} +

+ {{GetDeviceDataAttr(device.number,'PM2.5','description')}}

- PM2.5:{{GetDeviceDataAttr(device.number,'PM2.5','Value')}}{{ GetDeviceDataAttr(device.number,'PM2.5','Unit')}} + PM2.5:{{GetDeviceDataAttr(device.number,'PM2.5','value')}}{{ GetDeviceDataAttr(device.number,'PM2.5','unit')}}
@@ -227,13 +227,13 @@
-

- {{GetDeviceDataAttr(device.number,'光照度','Description')}} +

+ {{GetDeviceDataAttr(device.number,'光照度','description')}}

- 光照 {{GetDeviceDataAttr(device.number,'光照度','Value')}}{{ GetDeviceDataAttr(device.number,'光照度','Unit')}} + 光照 {{GetDeviceDataAttr(device.number,'光照度','value')}}{{ GetDeviceDataAttr(device.number,'光照度','unit')}}
@@ -245,12 +245,12 @@
- +

正常

- 红外 {{GetDeviceDataAttr(device.number,'状态','Value') }} + 红外 {{GetDeviceDataAttr(device.number,'状态','value') }}
@@ -262,12 +262,12 @@
- +

正常

- 烟雾 {{GetDeviceDataAttr(device.number,'状态','Value') }} + 烟雾 {{GetDeviceDataAttr(device.number,'状态','value') }}
@@ -333,7 +333,7 @@

- {{device.DisplayName||device.name}} + {{device.displayName||device.name}}

@@ -386,7 +386,7 @@

- {{device.DisplayName||device.name}} + {{device.displayName||device.name}}