diff --git a/projects/Demo/Demo/wwwroot/index.html b/projects/Demo/Demo/wwwroot/index.html index b1ab33de..e418619c 100644 --- a/projects/Demo/Demo/wwwroot/index.html +++ b/projects/Demo/Demo/wwwroot/index.html @@ -21,7 +21,7 @@ - title + {{Title}} @@ -30,8 +30,8 @@ - - - + + - + + + @@ -155,6 +145,13 @@ + diff --git a/projects/Demo/Demo/wwwroot/js/app.js b/projects/Demo/Demo/wwwroot/js/app.js index 36b1379a..552572b0 100644 Binary files a/projects/Demo/Demo/wwwroot/js/app.js and b/projects/Demo/Demo/wwwroot/js/app.js differ diff --git a/projects/Infrastructure/Infrastructure.csproj b/projects/Infrastructure/Infrastructure.csproj index aa18cce4..b6e41ff5 100644 --- a/projects/Infrastructure/Infrastructure.csproj +++ b/projects/Infrastructure/Infrastructure.csproj @@ -7,6 +7,7 @@ + diff --git a/projects/Infrastructure/Jwt/IJwtHelper.cs b/projects/Infrastructure/Jwt/IJwtHelper.cs new file mode 100644 index 00000000..3be5c328 --- /dev/null +++ b/projects/Infrastructure/Jwt/IJwtHelper.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Infrastructure.Jwt +{ + public interface IJwtHelper + { + string GetToken(IDictionary payload); + + IDictionary GetPayload(string token); + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Jwt/JwtHelper.cs b/projects/Infrastructure/Jwt/JwtHelper.cs new file mode 100644 index 00000000..ebf214ab --- /dev/null +++ b/projects/Infrastructure/Jwt/JwtHelper.cs @@ -0,0 +1,43 @@ +using JWT.Algorithms; +using JWT.Builder; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; + +namespace Infrastructure.Jwt +{ + public class JwtHelper : IJwtHelper + { + private readonly IConfiguration _cfg; + + public JwtHelper(IConfiguration cfg) + { + this._cfg = cfg; + } + + public IDictionary GetPayload(string token) + { + var secret = this._cfg["jwt:key"]; + var payload = new JwtBuilder() + .WithSecret(secret) + .MustVerifySignature() + .Decode>(token); + return payload; + } + + public string GetToken(IDictionary payload) + { + var secret = this._cfg["jwt:key"]; + var builder = new JwtBuilder() + .WithAlgorithm(new HMACSHA256Algorithm()) + .WithSecret(secret) + .AddClaim("exp", DateTimeOffset.UtcNow.AddYears(100).ToUnixTimeSeconds()); + foreach (var item in payload) + { + builder.AddClaim(item.Key, item.Value); + } + var token = builder.Build(); + return token; + } + } +} \ No newline at end of file diff --git a/projects/Infrastructure/Web/BaseStartup.cs b/projects/Infrastructure/Web/BaseStartup.cs index cb3bbb3f..50445717 100644 --- a/projects/Infrastructure/Web/BaseStartup.cs +++ b/projects/Infrastructure/Web/BaseStartup.cs @@ -1,4 +1,5 @@ using Infrastructure.Data; +using Infrastructure.Jwt; using Infrastructure.Office; using Infrastructure.Security; using Infrastructure.UI; @@ -133,6 +134,7 @@ namespace Infrastructure.Web services.AddSingleton(); services.AddTransient(typeof(IRepository<>), typeof(EfRepository<>)); services.AddTransient(); + services.AddTransient(); services.AddTransient(); } diff --git a/projects/IoTCenter/Controllers/HomeController.cs b/projects/IoTCenter/Controllers/HomeController.cs index 172fe91f..e32e2440 100644 --- a/projects/IoTCenter/Controllers/HomeController.cs +++ b/projects/IoTCenter/Controllers/HomeController.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; +using Infrastructure.Jwt; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -11,11 +12,13 @@ namespace IoTCenter.Controllers { public class HomeController : Controller { + private readonly IJwtHelper _jwtHelper; private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; - public HomeController(IRepository nodeRepo, IRepository deviceRepo) + public HomeController(IJwtHelper jwtHelper, IRepository nodeRepo, IRepository deviceRepo) { + this._jwtHelper = jwtHelper; this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; } @@ -26,8 +29,9 @@ namespace IoTCenter.Controllers return View(); } - public IActionResult GetNodes() + public IActionResult GetNodes(string token) { + var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString(); var model = this._nodeRepo.ReadOnlyTable() .Select(o => new { o.Number, o.Name, o.DisplayOrder, Count = o.Devices.Count }) .ToList(); diff --git a/projects/IoTCenter/Program.cs b/projects/IoTCenter/Program.cs index c4a17366..3dbfd16a 100644 --- a/projects/IoTCenter/Program.cs +++ b/projects/IoTCenter/Program.cs @@ -20,6 +20,7 @@ namespace IoTCenter new EFConfigurationValue { Id = "server.urls", Value= "http://*:8001" }, new EFConfigurationValue { Id = "security:key", Value= "111111111111111111111111"}, new EFConfigurationValue { Id = "security:iv", Value= "11111111"}, + new EFConfigurationValue { Id = "jwt:key", Value= "111111111111111111111111"}, new EFConfigurationValue { Id = "usercenter:key", Value= "123456"}, new EFConfigurationValue { Id = "usercenter:login", Value= $"http://{host}:8000/Account/Login"}, new EFConfigurationValue { Id = "usercenter:logout", Value= $"http://{host}:8000/Account/Logout"}, diff --git a/projects/UserCenter/Controllers/AccountController.cs b/projects/UserCenter/Controllers/AccountController.cs index cb30a70f..da160ace 100644 --- a/projects/UserCenter/Controllers/AccountController.cs +++ b/projects/UserCenter/Controllers/AccountController.cs @@ -3,11 +3,14 @@ using Application.Models; using Infrastructure.Data; using Infrastructure.Email; using Infrastructure.Extensions; +using Infrastructure.Jwt; using Infrastructure.Resources; using Infrastructure.Security; using Infrastructure.Sms; using Infrastructure.Web; using Infrastructure.Web.DataAnnotations; +using JWT.Algorithms; +using JWT.Builder; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -29,6 +32,7 @@ namespace UserCenter.Controllers public class AccountController : BaseController { private readonly IConfiguration _cfg; + private readonly IJwtHelper _jwtHelper; private readonly IRepository _userRepo; private readonly IRepository _siteRepo; private readonly IStringLocalizer _localizer; @@ -37,6 +41,7 @@ namespace UserCenter.Controllers private readonly ISmsSender _smsSender; public AccountController(IConfiguration cfg, + IJwtHelper jwtHelper, IRepository userRepo, IRepository siteRepo, IEncryptionService encryptionService, @@ -45,6 +50,7 @@ namespace UserCenter.Controllers ISmsSender smsSender) { this._cfg = cfg; + this._jwtHelper = jwtHelper; this._userRepo = userRepo; this._siteRepo = siteRepo; this._encryptionService = encryptionService; @@ -176,7 +182,19 @@ namespace UserCenter.Controllers if (success) { var list = this._siteRepo.ReadOnlyTable().ToList(); - if (!isAppLogin) + if (isAppLogin) + { + return Json(new + { + Code = 0, + Token = this._jwtHelper.GetToken(new Dictionary() { { nameof(user.UserName), user.UserName } }), + user.NickName, + Title = this._cfg["name"], + IoTServer = list.FirstOrDefault(o => o.Name == "物联网平台").Home + } + ); + } + else { var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) .SelectMany(o => o.UserRoles) @@ -206,10 +224,6 @@ namespace UserCenter.Controllers Response.Headers.Remove("Location"); return View("JsonpLogin", urls); } - else - { - return Json(new { Code = 0, NickName = user.NickName, IoTServer = list.FirstOrDefault(o => o.Name == "物联网平台").Home }); - } } else { @@ -232,15 +246,15 @@ namespace UserCenter.Controllers message = ex.Message; } } - if (!isAppLogin) + if (isAppLogin) { - ModelState.AddModelError(key, message); - ViewData["ReturnUrl"] = returnUrl; - return View(model); + return Json(new { Code = 1, Key = key, Message = message }); } else { - return Json(new { Code = 1, Key = key, Message = message }); + ModelState.AddModelError(key, message); + ViewData["ReturnUrl"] = returnUrl; + return View(model); } }