添加OAuth2认证相关配置文件和服务实现,包括环境变量配置、PKCE流程支持、token管理等功能。主要变更: - 新增OAuth2配置文件 - 实现OAuth2服务层 - 更新请求拦截器支持token自动刷新 - 修改认证API和store以支持OAuth2流程
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, true);
|
||
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("~/connect/logout")]
|
||
public IActionResult NotImplemented()
|
||
{
|
||
return RedirectToAction("Index", "Dashboard");
|
||
}
|
||
|
||
[HttpPost("~/connect/logout")]
|
||
[ValidateAntiForgeryToken]
|
||
public async Task<IActionResult> LogoutPost()
|
||
{
|
||
await _signInManager.SignOutAsync();
|
||
return Redirect("/");
|
||
}
|
||
}
|