using System; using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Email; using Infrastructure.Extensions; using Infrastructure.Resources; using Infrastructure.Security; using Infrastructure.Sms; using Infrastructure.Web; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Localization; namespace IoTCenter.Controllers { [Authorize] public class AccountController : BaseController { private readonly IConfiguration _configuration; private readonly IRepository _userRepo; private readonly IStringLocalizer _localizer; private readonly IEncryptionService _encryptionService; private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; public AccountController(IConfiguration configuration, IRepository userRepo, IEncryptionService encryptionService, IStringLocalizer localizer, IEmailSender emaliSender, ISmsSender smsSender) { this._configuration = configuration; this._userRepo = userRepo; this._encryptionService = encryptionService; this._localizer = localizer; this._emailSender = emaliSender; this._smsSender = smsSender; } #region 权限不足 [AllowAnonymous] public IActionResult AccessDenied(string returnUrl) { return View(model: returnUrl); } #endregion 权限不足 public IActionResult Index() { return View(); } public IActionResult Logout() { var fullReturnUrl = Url.FullAction("Index", "Home"); var logoutUrl = this._configuration["usercenter:logout"]; var url = logoutUrl.SetParam("returnUrl", fullReturnUrl); return Redirect(url); } public string JsonpLogout(string userName, string timestamp, string sign) { try { var key = this._configuration["usercenter:key"]; if (string.Concat(userName, timestamp, key).Md5() == sign) { HttpContext.SignOutAsync(); return ""; } else { return $"function(){{return \"{userName} logout error\";}}();"; } } catch (Exception ex) { ex.PrintStack(); return $"function(){{return {ex.Message};}}();"; } } [HttpGet] [AllowAnonymous] public IActionResult Login(string returnUrl = null) { var fullReturnUrl = Url.GetFullUrl(returnUrl ?? "~"); var loginUrl = this._configuration["usercenter:login"]; var url = loginUrl.SetParam(nameof(returnUrl), fullReturnUrl); return Redirect(url); } [AllowAnonymous] 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() { var fullReturnUrl = Url.FullAction("Index", "Home"); var registerUrl = this._configuration["usercenter:register"]; var url = registerUrl.SetParam("returnUrl", fullReturnUrl); return Redirect(url); } [Route("/Login")] [AllowAnonymous] [ApiExplorerSettings(IgnoreApi = true)] public IActionResult Test() { var userName = "admin"; 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); return RedirectToAction("Index", "Home"); } } }