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.
118 lines
3.6 KiB
118 lines
3.6 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Controllers
|
|
{
|
|
[Authorize]
|
|
public class AccountController : BaseController
|
|
{
|
|
private readonly IConfiguration _cfg;
|
|
private readonly IRepository<User> _userRepo;
|
|
|
|
public AccountController(IConfiguration cfg,
|
|
IRepository<User> userRepo)
|
|
{
|
|
this._cfg = cfg;
|
|
this._userRepo = userRepo;
|
|
}
|
|
|
|
#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]
|
|
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};}}();";
|
|
}
|
|
}
|
|
|
|
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]
|
|
public IActionResult Register()
|
|
{
|
|
var fullReturnUrl = Url.FullAction("Index", "Home");
|
|
var registerUrl = this._cfg["usercenter:register"];
|
|
var url = registerUrl.SetParam("returnUrl", fullReturnUrl);
|
|
return Redirect(url);
|
|
}
|
|
}
|
|
} |