步骤
1、RegisterDTO
CitiesManager.Core中新建文件夹DTO,DTO中新建类RegisterDTO.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CitiesManager.Core.DTO
{
public class RegisterDTO
{
[Required(ErrorMessage = "Person Name can't be blank")]
public string PersonName { get; set; } = string.Empty;
[Required(ErrorMessage = "Email can't be blank")]
[EmailAddress(ErrorMessage = "Email should be in a proper email address format")]
[Remote(action: "IsEmailAlreadyRegistered", controller: "Account", ErrorMessage = "Email is already in use")]
public string Email { get; set; } = string.Empty;
[Required(ErrorMessage = "PhoneNumber can't be blank")]
[RegularExpression("^[0-9]*$", ErrorMessage = "PhoneNumber should contain digits only")]
public string PhoneNumber { get; set; } = string.Empty;
[Required(ErrorMessage = "Password can't be blank")]
public string Password { get; set; } = string.Empty;
[Required(ErrorMessage = "Confirm Password can't be blank")]
[Compare("Password", ErrorMessage = "Password and Confirm Password should be same")]
public string ConfirmPassword { get; set; } = string.Empty;
}
}
2、AccountController
using CitiesManager.Core.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using CitiesManager.Core.DTO;
using System.ComponentModel.DataAnnotations;
namespace CitiesManager.WebAPI.Controllers.v1
{
[AllowAnonymous]
[ApiVersion("1.0")]
public class AccountController : CustomControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly RoleManager<ApplicationRole> _roleManager;
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, RoleManager<ApplicationRole> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}
[HttpPost("register")]
public async Task<ActionResult<ApplicationUser>> PostRegister(RegisterDTO registerDTO)
{
//Validation
if (ModelState.IsValid == false)
{
string errorMessage = string.Join("|", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
return Problem(errorMessage);
}
//Create user
ApplicationUser user = new ApplicationUser()
{
Email = registerDTO.Email,
PhoneNumber = registerDTO.PhoneNumber,
UserName = registerDTO.Email,
PersonName = registerDTO.PersonName
};
IdentityResult identityResult = await _userManager.CreateAsync(user, registerDTO.Password);
if (identityResult.Succeeded)
{
await _signInManager.SignInAsync(user, false);
return Ok(user);
}
else
{
string errorMessage = string.Join("|", identityResult.Errors.Select(e => e.Description));
return Problem(errorMessage);
}
}
[HttpGet]
public async Task<IActionResult> IsEmailAlreadyRegistered(string email)
{
ApplicationUser? user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return Ok(true);
}
else
{
return Ok(false);
}
}
}
}
Program.cs中添加UseStaticFiles()
// Configure the HTTP request pipeline.
app.UseHsts(); //Force to user https
app.UseHttpsRedirection();
app.UseStaticFiles();
Gitee获取源码: