Former-commit-id: b971c7668c31400f6c93f84544dd760b05e51c1f
TangShanKaiPing
wanggang 6 years ago
parent 75766fa831
commit 56d56bec7b

@ -14,14 +14,6 @@ namespace Infrastructure.Extensions
{
public static class HttpContextExtensions
{
public static void SignIn(this HttpContext httpContext, string userName, IEnumerable<string> roles, bool rememberMe)
{
var claims = new List<Claim> { 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));

@ -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<IRepository<User>>();
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)

@ -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<string>("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<string>("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()

@ -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");
}
}

@ -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<User> _userRepo;
public RoleService(IRepository<User> userRepo)
{
this._userRepo = userRepo;
}
public List<string> 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;
}
}
}

@ -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<IRoleService, RoleService>();
services.AddTransient<IEmailSender, EmptyEmailSender>();
services.AddTransient<IoTNodeJob>();
services.AddSingleton<IoTNodeClient>();

@ -45,27 +45,6 @@ namespace UserCenter
app.ApplicationServices.GetRequiredService<FaceRecognitionService>();
}
public override Task ValidatePrincipal(CookieValidatePrincipalContext arg)
{
return Task.Run(() =>
{
var userRepo = arg.HttpContext.RequestServices.GetService<IRepository<User>>();
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<PermissionCategory>().HasOne(o => o.Parent).WithMany(o => o.Children).HasForeignKey(o => o.ParentId).OnDelete(DeleteBehavior.SetNull);

@ -7,7 +7,7 @@
<link rel="stylesheet" href="lib/weui/weui.min.css">
<link rel="stylesheet" href="lib/ionicons/css/ionicons.min.css">
<link rel="stylesheet" href="css/app.css">
<title>物联网中心</title>
<title>首页</title>
</head>
<body>
<div class="weui-toptips weui-toptips_warn js_tooltips">错误提示</div>
@ -26,12 +26,11 @@
<script src="lib/weui.js/weui.min.js"></script>
<script src="lib/vue/vue.min.js"></script>
<script src="lib/vue-router/vue-router.min.js"></script>
<script src="lib/signalr/signalr.min.js"></script>
<!---->
<script src="lib/mint-ui/index.js"></script>
<!---->
<script src="UserCenter/Account/HasLogin"></script>
<script src="js/app.js"></script>
</body>
</html>
Loading…
Cancel
Save