diff --git a/projects/Infrastructure/Extensions/HttpContextExtensions.cs b/projects/Infrastructure/Extensions/HttpContextExtensions.cs index aaeb3ab0..1b613a9a 100644 --- a/projects/Infrastructure/Extensions/HttpContextExtensions.cs +++ b/projects/Infrastructure/Extensions/HttpContextExtensions.cs @@ -14,14 +14,6 @@ namespace Infrastructure.Extensions { public static class HttpContextExtensions { - public static void SignIn(this HttpContext httpContext, string userName, IEnumerable roles, bool rememberMe) - { - var claims = new List { new Claim("Name", userName) }; - claims.AddRange(roles.Select(o => new Claim("Role", o)).ToList()); - var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme, "Name", "Role")); - httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = rememberMe }); - } - public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg) { var token = httpContext.GetToken(userName, cfg, DateTime.Now.AddYears(1)); diff --git a/projects/IoT.Shared/IoTServiceStartup.cs b/projects/IoT.Shared/IoTServiceStartup.cs index 7b9b6aa3..be4d39d6 100644 --- a/projects/IoT.Shared/IoTServiceStartup.cs +++ b/projects/IoT.Shared/IoTServiceStartup.cs @@ -38,28 +38,6 @@ namespace IoT.UI.Shard base.ConfigureServices(services); } - public override Task ValidatePrincipal(CookieValidatePrincipalContext arg) - { - return Task.Run(() => - { - var userRepo = arg.HttpContext.RequestServices.GetService>(); - - var userName = arg.Principal.Identity.Name; - var userPermissions = userRepo.ReadOnlyTable().Where(o => o.UserName == userName) - .SelectMany(o => o.UserRoles) - .Select(o => o.Role) - .SelectMany(o => o.RolePermissions) - .Select(o => o.Permission.Number) - .ToList(); - var currentPermissions = arg.Principal.Claims.Where(o => o.Type == "Role").Select(o => o.Value).ToList(); - if (!currentPermissions.SequenceEqual(userPermissions)) - { - arg.HttpContext.SignOutAsync(); - arg.HttpContext.SignIn(userName, userPermissions, arg.Properties.IsPersistent); - } - }); - } - public override void OnModelCreating(ModelBuilder modelBuilder) { if (modelBuilder == null) diff --git a/projects/IoTCenter/Controllers/AccountController.cs b/projects/IoTCenter/Controllers/AccountController.cs index f4a85a6a..68636767 100644 --- a/projects/IoTCenter/Controllers/AccountController.cs +++ b/projects/IoTCenter/Controllers/AccountController.cs @@ -97,40 +97,40 @@ namespace IoTCenter.Controllers return Redirect(url); } - [AllowAnonymous] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] - public string JsonpLogin(string userName, string timestamp, bool rememberMe, string sign) - { - try - { - var key = this._configuration.GetSection("usercenter").GetValue("key"); - if (string.Concat(userName, timestamp, key).Md5() == sign) - { - if (!this._userRepo.ReadOnlyTable().Any(o => o.UserName == userName)) - { - this._userRepo.Add(new User { UserName = userName }); - this._userRepo.SaveChanges(); - } - var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) - .SelectMany(o => o.UserRoles) - .Select(o => o.Role) - .SelectMany(o => o.RolePermissions) - .Select(o => o.Permission.Number) - .ToList(); - HttpContext.SignIn(userName, userPermissions, rememberMe); - return ""; - } - else - { - return $"function(){{return \"{userName} login error\";}}();"; - } - } - catch (Exception ex) - { - ex.PrintStack(); - return $"function(){{return {ex.Message};}}();"; - } - } + //[AllowAnonymous] + //[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")] + //public string JsonpLogin(string userName, string timestamp, bool rememberMe, string sign) + //{ + // try + // { + // var key = this._configuration.GetSection("usercenter").GetValue("key"); + // if (string.Concat(userName, timestamp, key).Md5() == sign) + // { + // if (!this._userRepo.ReadOnlyTable().Any(o => o.UserName == userName)) + // { + // this._userRepo.Add(new User { UserName = userName }); + // this._userRepo.SaveChanges(); + // } + // var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) + // .SelectMany(o => o.UserRoles) + // .Select(o => o.Role) + // .SelectMany(o => o.RolePermissions) + // .Select(o => o.Permission.Number) + // .ToList(); + // HttpContext.SignIn(userName, userPermissions, rememberMe); + // return ""; + // } + // else + // { + // return $"function(){{return \"{userName} login error\";}}();"; + // } + // } + // catch (Exception ex) + // { + // ex.PrintStack(); + // return $"function(){{return {ex.Message};}}();"; + // } + //} [AllowAnonymous] public IActionResult Register() diff --git a/projects/IoTNode/Controllers/AccountController.cs b/projects/IoTNode/Controllers/AccountController.cs index 0fa894a8..cd57517e 100644 --- a/projects/IoTNode/Controllers/AccountController.cs +++ b/projects/IoTNode/Controllers/AccountController.cs @@ -95,12 +95,6 @@ namespace IoTNode.Controllers { if (user.PasswordHash == this._encryptionService.CreatePasswordHash(password, user.SecurityStamp)) { - var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) - .SelectMany(o => o.UserRoles) - .Select(o => o.Role) - .SelectMany(o => o.RolePermissions) - .Select(o => o.Permission.Number) - .ToList(); HttpContext.SignIn(model.UserName, model.RememberMe, _cfg); if (string.IsNullOrEmpty(returnUrl)) { @@ -307,13 +301,7 @@ namespace IoTNode.Controllers public IActionResult Test() { var userName = "super"; - var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) - .SelectMany(o => o.UserRoles) - .Select(o => o.Role) - .SelectMany(o => o.RolePermissions) - .Select(o => o.Permission.Number) - .ToList(); - HttpContext.SignIn(userName, userPermissions, true); + HttpContext.SignIn(userName, true, _cfg); return RedirectToAction("Index", "Home"); } } diff --git a/projects/IoTNode/RoleService.cs b/projects/IoTNode/RoleService.cs new file mode 100644 index 00000000..87b31cb4 --- /dev/null +++ b/projects/IoTNode/RoleService.cs @@ -0,0 +1,29 @@ +using Application.Domain.Entities; +using Infrastructure.Data; +using Infrastructure.Web; +using System.Collections.Generic; +using System.Linq; + +namespace IoTNode +{ + public class RoleService : IRoleService + { + private readonly IRepository _userRepo; + + public RoleService(IRepository userRepo) + { + this._userRepo = userRepo; + } + + public List GetRoles(string userName) + { + var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName) + .SelectMany(o => o.UserRoles) + .Select(o => o.Role) + .SelectMany(o => o.RolePermissions) + .Select(o => o.Permission.Number) + .ToList(); + return userPermissions; + } + } +} \ No newline at end of file diff --git a/projects/IoTNode/Startup.cs b/projects/IoTNode/Startup.cs index 0d6e547f..33e58a6d 100644 --- a/projects/IoTNode/Startup.cs +++ b/projects/IoTNode/Startup.cs @@ -18,6 +18,7 @@ using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Logging; using IoTNode.DeviceServices.SerialPortManager; +using Infrastructure.Web; namespace IoTNode { @@ -29,6 +30,7 @@ namespace IoTNode public override void ConfigureServices(IServiceCollection services) { + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); diff --git a/projects/UserCenter/Startup.cs b/projects/UserCenter/Startup.cs index c676d6c0..0977b3f9 100644 --- a/projects/UserCenter/Startup.cs +++ b/projects/UserCenter/Startup.cs @@ -45,27 +45,6 @@ namespace UserCenter app.ApplicationServices.GetRequiredService(); } - public override Task ValidatePrincipal(CookieValidatePrincipalContext arg) - { - return Task.Run(() => - { - var userRepo = arg.HttpContext.RequestServices.GetService>(); - - var userName = arg.Principal.Identity.Name; - var userPermissions = userRepo.ReadOnlyTable().Where(o => o.UserName == userName) - .SelectMany(o => o.UserRoles) - .Select(o => o.Role) - .SelectMany(o => o.RolePermissions) - .Select(o => o.Permission.Number) - .ToList(); - var currentPermissions = arg.Principal.Claims.Where(o => o.Type == "Role").Select(o => o.Value).ToList(); - if (!currentPermissions.SequenceEqual(userPermissions)) - { - arg.HttpContext.SignOutAsync(); - arg.HttpContext.SignIn(userName, userPermissions, arg.Properties.IsPersistent); - } - }); - } public override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId).OnDelete(DeleteBehavior.SetNull); diff --git a/projects/WebUI/wwwroot/index.html b/projects/WebUI/wwwroot/index.html index 2c1b1235..504e2f35 100644 --- a/projects/WebUI/wwwroot/index.html +++ b/projects/WebUI/wwwroot/index.html @@ -7,7 +7,7 @@ - 物联网中心 + 首页
错误提示
@@ -26,12 +26,11 @@ - + - \ No newline at end of file