2025-07-08 23:06:19 +08:00
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
|
|
#nullable disable
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
2025-07-11 16:17:31 +08:00
|
|
|
using System.Security.Claims;
|
2025-07-08 23:06:19 +08:00
|
|
|
using System.Text;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using AGSS.Models.Entities;
|
|
|
|
using AGSS.Utilities;
|
2025-07-11 16:17:31 +08:00
|
|
|
using asg_form;
|
2025-07-08 23:06:19 +08:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-11 16:17:31 +08:00
|
|
|
using Microsoft.Extensions.Options;
|
2025-07-08 23:06:19 +08:00
|
|
|
|
|
|
|
namespace AGSS.Areas.Identity.Pages.Account
|
|
|
|
{
|
|
|
|
public class RegisterModel : PageModel
|
|
|
|
{
|
|
|
|
private readonly SignInManager<UserModel> _signInManager;
|
|
|
|
private readonly UserManager<UserModel> _userManager;
|
|
|
|
private readonly IUserStore<UserModel> _userStore;
|
|
|
|
private readonly IUserEmailStore<UserModel> _emailStore;
|
|
|
|
private readonly ILogger<RegisterModel> _logger;
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
private readonly Jwt _jwt;
|
|
|
|
|
|
|
|
public RegisterModel(
|
|
|
|
UserManager<UserModel> userManager,
|
|
|
|
IUserStore<UserModel> userStore,
|
|
|
|
SignInManager<UserModel> signInManager,
|
|
|
|
ILogger<RegisterModel> logger,
|
|
|
|
IEmailSender emailSender,
|
|
|
|
Jwt jwt
|
|
|
|
)
|
|
|
|
|
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_userStore = userStore;
|
|
|
|
_emailStore = GetEmailStore();
|
|
|
|
_signInManager = signInManager;
|
|
|
|
_logger = logger;
|
|
|
|
_emailSender = emailSender;
|
|
|
|
_jwt = jwt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
[BindProperty]
|
|
|
|
public InputModel Input { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
public string ReturnUrl { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
public IList<AuthenticationScheme> ExternalLogins { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
public class InputModel
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
[Required]
|
|
|
|
[EmailAddress]
|
|
|
|
[Display(Name = "Email")]
|
|
|
|
public string Email { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
[Required]
|
|
|
|
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
|
|
|
|
[DataType(DataType.Password)]
|
|
|
|
[Display(Name = "Password")]
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
|
|
|
/// directly from your code. This API may change or be removed in future releases.
|
|
|
|
/// </summary>
|
|
|
|
[DataType(DataType.Password)]
|
|
|
|
[Display(Name = "Confirm password")]
|
|
|
|
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
|
|
|
|
public string ConfirmPassword { get; set; }
|
2025-07-09 23:13:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
[MaxLength(10)]
|
|
|
|
[Display(Name = "Confirm password")]
|
|
|
|
public string Sex { get; set; }
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task OnGetAsync(string returnUrl = null)
|
|
|
|
{
|
|
|
|
ReturnUrl = returnUrl;
|
|
|
|
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
|
|
|
}
|
|
|
|
|
2025-07-11 16:17:31 +08:00
|
|
|
public async Task<IActionResult> OnPostAsync([FromServices] IOptions<JWTOptions> jwtOptions,string returnUrl = null)
|
2025-07-08 23:06:19 +08:00
|
|
|
{
|
|
|
|
returnUrl ??= Url.Content("~/");
|
|
|
|
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var user = CreateUser();
|
2025-07-09 23:13:11 +08:00
|
|
|
user.Id = Guid.NewGuid().ToString();
|
|
|
|
user.Sex = Input.Sex;
|
2025-07-08 23:06:19 +08:00
|
|
|
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
|
|
|
|
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
|
|
|
|
var result = await _userManager.CreateAsync(user, Input.Password);
|
|
|
|
|
|
|
|
if (result.Succeeded)
|
|
|
|
{
|
|
|
|
_logger.LogInformation("User created a new account with password.");
|
2025-07-11 16:17:31 +08:00
|
|
|
// var roles = await _userManager.GetRolesAsync(user);
|
|
|
|
var claims = new List<Claim>();
|
|
|
|
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
|
|
|
|
claims.Add(new Claim(ClaimTypes.Name, user.UserName));
|
2025-07-09 13:57:43 +08:00
|
|
|
var roles = await _userManager.GetRolesAsync(user);
|
2025-07-11 16:17:31 +08:00
|
|
|
foreach (string role in roles)
|
|
|
|
{
|
|
|
|
claims.Add(new Claim(ClaimTypes.Role, role));
|
|
|
|
}
|
|
|
|
string jwtToken = _jwt.BuildToken(claims, jwtOptions.Value);
|
|
|
|
var frontendCallback = $"{Request.Query["frontendCallback"]}?token={jwtToken}";
|
2025-07-08 23:06:19 +08:00
|
|
|
|
|
|
|
return Redirect(frontendCallback);
|
|
|
|
}
|
|
|
|
foreach (var error in result.Errors)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(string.Empty, error.Description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, something failed, redisplay form
|
|
|
|
return Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
private UserModel CreateUser()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return Activator.CreateInstance<UserModel>();
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"Can't create an instance of '{nameof(UserModel)}'. " +
|
|
|
|
$"Ensure that '{nameof(UserModel)}' is not an abstract class and has a parameterless constructor, or alternatively " +
|
|
|
|
$"override the register page in /Areas/Identity/Pages/Account/Register.cshtml");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IUserEmailStore<UserModel> GetEmailStore()
|
|
|
|
{
|
|
|
|
if (!_userManager.SupportsUserEmail)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException("The default UI requires a user store with email support.");
|
|
|
|
}
|
|
|
|
return (IUserEmailStore<UserModel>)_userStore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|