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.
107 lines
3.4 KiB
107 lines
3.4 KiB
using System;
|
|
using System.Linq;
|
|
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Email;
|
|
using Infrastructure.Extensions;
|
|
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 StudyCenter.Controllers
|
|
{
|
|
[Authorize]
|
|
public class AccountController : BaseController
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IRepository<User> _userRepo;
|
|
private readonly IStringLocalizer<Infrastructure.Resources.Resource> _localizer;
|
|
private readonly IEncryptionService _encryptionService;
|
|
private readonly IEmailSender _emailSender;
|
|
private readonly ISmsSender _smsSender;
|
|
|
|
public AccountController(IConfiguration configuration,
|
|
IRepository<User> userRepo,
|
|
IEncryptionService encryptionService,
|
|
IStringLocalizer<Infrastructure.Resources.Resource> 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 IActionResult Register()
|
|
{
|
|
var fullReturnUrl = Url.FullAction("Index", "Home");
|
|
var registerUrl = this._configuration["usercenter:register"];
|
|
var url = registerUrl.SetParam("returnUrl", fullReturnUrl);
|
|
return Redirect(url);
|
|
}
|
|
}
|
|
} |