我们将使用RegisterViewModel类作为Register视图的模型,它负责将视图中的信息传递给控制器。为了验证信息是否正确,我们使用了几个http://ASP.NET Core验证属性。在之前的章节中详细说明过这些属性和模型验证。
using System.ComponentModel.DataAnnotations;
namespace MockSchoolManagement.ViewModels
{
public class RegisterViewModel
{
账户控制器(AccountController)是指所有与账户相关的CRUD(增加、读取、更新和删除)操作都将在此控制器中。目前我们只有Register()操作方法,可以通过向/account/register发出GET请求来实现此操作方法。
using Microsoft.AspNetCore.Mvc;
namespace MockSchoolManagement.Controllers
{
public class AccountController:Controller
{
[HttpGet]
public IActionResult Register()
{
return View();
}
}
}21.2.3 注册视图中的代码
以下是AccountController的完整代码。
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using MockSchoolManagement.ViewModels;
using System.Threading.Tasks;
namespace MockSchoolManagement.Controllers
{
public class AccountController:Controller
{
private UserManager<IdentityUser> _userManager;
private SignInManager<IdentityUser> _signInManager;
在http://ASP.NET Core Identity中,密码默认设置在PasswordOptions类中。读者可以在http://ASP.NET Core GitHub仓库中找到此类的源代码。只需在仓库中搜索PasswordOptions类。
代码如下。
public class PasswordOptions
{
public int RequiredLength{get;set;} = 6;
public int RequiredUniqueChars{get;set;} = 1;
public bool RequireNonAlphanumeric{get;set;} = true;
public bool RequireLowercase{get;set;} = true;
public bool RequireUppercase{get;set;} = true;
public bool RequireDigit{get;set;} = true;
}相关参数的说明如表21.1(略)所示。
21.3.3 覆盖http://ASP.NET Core身份中的密码默认设置
Identity提供了AddErrorDescriber()方法,可方便我们进行错误内容的配置和处理。
http://ASP.NET Core默认提供的都是英文提示,我们可以将它们修改为中文。现在我们创建一个CustomIdentityErrorDescriber的类文件,路径为根目录下创建的CustomerMiddlewares文件夹,然后继承IdentityErrorDescriber服务,添加以下代码。
public class CustomIdentityErrorDescriber:IdentityErrorDescriber
{
public override IdentityError DefaultError()
{
return new IdentityError{Code = nameof(DefaultError),Description = $&#34;发生了未知的故障。&#34; };
}
public override IdentityError ConcurrencyFailure()
{
return new IdentityError{Code = nameof(ConcurrencyFailure),Description = &#34;乐观并发失败,对象已被修改。&#34; };
}
public override IdentityError PasswordMismatch()
{
return new IdentityError{Code = nameof(PasswordMismatch),Description = &#34;密码错误&#34; };
}
public override IdentityError InvalidToken()
{
return new IdentityError{Code = nameof(InvalidToken),Description = &#34;无效的令牌.&#34; };
}
public override IdentityError LoginAlreadyAssociated()
{
return new IdentityError{Code = nameof(LoginAlreadyAssociated),Description = &#34;具有此登录的用户已经存在.&#34; };
}
public override IdentityError InvalidUserName(string userName)
{
return new IdentityError{Code = nameof(InvalidUserName),Description = $&#34;用户名&#39;{userName}&#39;无效,只能包含字母或数字.&#34; };
}
public override IdentityError InvalidEmail(string email)
{
return new IdentityError{Code = nameof(InvalidEmail),Description = $&#34;邮箱&#39;{email}&#39;无效.&#34; };
}
public override IdentityError DuplicateUserName(string userName)
{
return new IdentityError{Code = nameof(DuplicateUserName),Description = $&#34;用户名&#39;{userName}&#39;已被使用.&#34; };
}
public override IdentityError DuplicateEmail(string email)
{
return new IdentityError{Code = nameof(DuplicateEmail),Description = $&#34;邮箱&#39;{email}&#39;已被使用.&#34; };
}
public override IdentityError InvalidRoleName(string role)
{
return new IdentityError{Code = nameof(InvalidRoleName),Description = $&#34;角色名&#39;{role}&#39;无效.&#34; };
}
public override IdentityError DuplicateRoleName(string role)
{
return new IdentityError{Code = nameof(DuplicateRoleName),Description = $&#34;角色名&#39;{role}&#39;已被使用.&#34; };
}
public override IdentityError UserAlreadyHasPassword()
{
return new IdentityError{Code = nameof(UserAlreadyHasPassword),Description = &#34;该用户已设置了密码.&#34; };
}
public override IdentityError UserLockoutNotEnabled()
{
return new IdentityError{Code = nameof(UserLockoutNotEnabled),Description = &#34;此用户未启用锁定.&#34; };
}
public override IdentityError UserAlreadyInRole(string role)
{
return new IdentityError{Code = nameof(UserAlreadyInRole),Description = $&#34;用户已关联角色&#39;{role}&#39;.&#34; };
}
public override IdentityError UserNotInRole(string role)
{
return new IdentityError{Code = nameof(UserNotInRole),Description = $&#34;用户未关联角色&#39;{role}&#39;.&#34; };
}
public override IdentityError PasswordTooShort(int length)
{
return new IdentityError{Code = nameof(PasswordTooShort),Description = $&#34;密码必须至少是{length}字符.&#34; };
}
public override IdentityError PasswordRequiresNonAlphanumeric()
{
return new IdentityError
{
Code = nameof(PasswordRequiresNonAlphanumeric),
Description = &#34;密码必须至少有一个非字母数字字符.&#34;
};
}
public override IdentityError PasswordRequiresDigit()
{
return new IdentityError{Code = nameof(PasswordRequiresDigit),Description = $&#34;密码必须至少有一个数字(&#39;0&#39;-&#39;9&#39;).&#34; };
}
public override IdentityError PasswordRequiresUniqueChars(int uniqueChars)
{
return new IdentityError{Code = nameof(PasswordRequiresUniqueChars),Description = $&#34;密码必须使用至少不同的{uniqueChars}字符。&#34; };
}
public override IdentityError PasswordRequiresLower()
{
return new IdentityError{Code = nameof(PasswordRequiresLower),Description = &#34;密码必须至少有一个小写字母(&#39;a&#39;-&#39;z&#39;).&#34; };
}
public override IdentityError PasswordRequiresUpper()
{
return new IdentityError{Code = nameof(PasswordRequiresUpper),Description = &#34;密码必须至少有一个大写字母(&#39;A&#39;-&#39;Z&#39;).&#34; };
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if(ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(
model.Email,model.Password,model.RememberMe,false);