134 lines
3.9 KiB
C#
134 lines
3.9 KiB
C#
using Fengling.AuthService.Data;
|
|
using Fengling.AuthService.Models;
|
|
using Fengling.AuthService.ViewModels;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Fengling.AuthService.Controllers;
|
|
|
|
[Route("account")]
|
|
public class AccountController : Controller
|
|
{
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
|
private readonly ApplicationDbContext _dbContext;
|
|
private readonly ILogger<AccountController> _logger;
|
|
|
|
public AccountController(
|
|
UserManager<ApplicationUser> userManager,
|
|
SignInManager<ApplicationUser> signInManager,
|
|
ApplicationDbContext dbContext,
|
|
ILogger<AccountController> logger)
|
|
{
|
|
_userManager = userManager;
|
|
_signInManager = signInManager;
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("login")]
|
|
public IActionResult Login(string returnUrl = "/")
|
|
{
|
|
return View(new LoginInputModel { ReturnUrl = returnUrl });
|
|
}
|
|
|
|
[HttpPost("login")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Login(LoginInputModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View(model);
|
|
}
|
|
|
|
var user = await _userManager.FindByNameAsync(model.Username);
|
|
if (user == null || user.IsDeleted)
|
|
{
|
|
ModelState.AddModelError(string.Empty, "用户名或密码错误");
|
|
return View(model);
|
|
}
|
|
|
|
var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, false);
|
|
if (!result.Succeeded)
|
|
{
|
|
if (result.IsLockedOut)
|
|
{
|
|
ModelState.AddModelError(string.Empty, "账号已被锁定");
|
|
}
|
|
else
|
|
{
|
|
ModelState.AddModelError(string.Empty, "用户名或密码错误");
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
return LocalRedirect(model.ReturnUrl);
|
|
}
|
|
|
|
[HttpGet("register")]
|
|
public IActionResult Register(string returnUrl = "/")
|
|
{
|
|
return View(new RegisterViewModel { ReturnUrl = returnUrl });
|
|
}
|
|
|
|
[HttpPost("register")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Register(RegisterViewModel model)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View(model);
|
|
}
|
|
|
|
var defaultTenant = await _dbContext.Tenants
|
|
.FirstOrDefaultAsync(t => t.TenantId == "default");
|
|
|
|
if (defaultTenant == null)
|
|
{
|
|
ModelState.AddModelError(string.Empty, "系统配置错误:未找到默认租户");
|
|
return View(model);
|
|
}
|
|
|
|
var user = new ApplicationUser
|
|
{
|
|
UserName = model.Username,
|
|
Email = model.Email,
|
|
NormalizedUserName = model.Username.ToUpper(),
|
|
NormalizedEmail = model.Email.ToUpper(),
|
|
TenantInfo = new TenantInfo(defaultTenant.Id, defaultTenant.TenantId, defaultTenant.Name)
|
|
};
|
|
|
|
var result = await _userManager.CreateAsync(user, model.Password);
|
|
if (!result.Succeeded)
|
|
{
|
|
foreach (var error in result.Errors)
|
|
{
|
|
ModelState.AddModelError(string.Empty, error.Description);
|
|
}
|
|
return View(model);
|
|
}
|
|
|
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
|
return LocalRedirect(model.ReturnUrl);
|
|
}
|
|
|
|
[HttpGet("profile")]
|
|
[HttpGet("settings")]
|
|
[HttpGet("logout")]
|
|
public IActionResult NotImplemented()
|
|
{
|
|
return RedirectToAction("Index", "Dashboard");
|
|
}
|
|
|
|
[HttpPost("logout")]
|
|
[ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> LogoutPost()
|
|
{
|
|
await _signInManager.SignOutAsync();
|
|
return Redirect("/");
|
|
}
|
|
}
|