You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Controllers/AccountController.cs

173 lines
6.3 KiB

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;
using System;
using System.Linq;
namespace IoTCenter.Controllers
{
[Authorize]
public class AccountController : BaseController
{
private readonly IConfiguration _cfg;
private readonly IRepository<User> _userRepo;
private readonly IStringLocalizer<Resource> _localizer;
private readonly IEncryptionService _encryptionService;
private readonly IEmailSender _emailSender;
private readonly ISmsSender _smsSender;
public AccountController(IConfiguration cfg,
IRepository<User> userRepo,
IEncryptionService encryptionService,
IStringLocalizer<Resource> localizer,
IEmailSender emaliSender,
ISmsSender smsSender)
{
this._cfg = cfg;
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 logoutUrl = this._cfg["usercenter:logout"];
var url = logoutUrl.SetParam("returnUrl", "/IoTCenter/");
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<string>("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._cfg["usercenter:key"];
if (string.Concat(userName, timestamp, key).Md5() == sign)
{
HttpContext.JwtSignOut();
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 loginUrl = this._cfg["usercenter:login"];
var url = loginUrl.SetParam(nameof(returnUrl), returnUrl ?? "/IoTCenter/");
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]
public IActionResult Register()
{
var fullReturnUrl = Url.FullAction("Index", "Home");
var registerUrl = this._cfg["usercenter:register"];
var url = registerUrl.SetParam("returnUrl", fullReturnUrl);
return Redirect(url);
}
}
}