2018教程之mvc+ef之二:更改Mail登录为UserName登记并让注册密码简单化
这章先简单解决两个问题:
1.注册和登录使用Email,个人感觉 不喜欢,好复杂,难得输。
2.在注册时,默认使用大小写,加符号太过于复杂,我想简化。当然可以不改
一、让注册密码简单化
App_Start/IdentityConfig.cs文件中 ApplicationUserManager.Create 静态方法中:
// 配置密码的验证逻辑
manager.PasswordValidator = new PasswordValidator
{
///长度为6
RequiredLength = 6,
///必须有非字母数字字符
RequireNonLetterOrDigit = false,
///必须有数字
RequireDigit = false,
///必须有小写字毒
RequireLowercase = false,
///必须有大写字毒
RequireUppercase = false,
};
我将他全部改为false
重新生成解决方案后,运行测试。测试成功。
二、更改UserName方式进行注册
1.打开AccountViewModels.cs,找到RegisterViewModel中的注册模型的更改。
public class RegisterViewModel
{
#region 扩展
[Display(Name = "用户名")]
public string UserName { get; set; }
[Display(Name = "电话(手机/固话)")]
[Phone]
public string PhoneNumber { get; set; }
#endregion
[Required]
[EmailAddress]
[Display(Name = "电子邮件")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
}
2. 然后我们修改Register.cshtml 添加如下代码:下面的代码我们放在邮箱的上面。
@Html.AntiForgeryToken()
<h4>创建新帐户。</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.PhoneNumber, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.PhoneNumber, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
3.修改 Register方法。AccountController.cs中找到Register方法
因为我加入 一个创建时间的datetime非空属性,要取当时的时候,同时我搞了个头像,注册时默认的为某个图像。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var _creatTime = DateTime.Now;
var _headerPic = "/Content/noheaderpic.png";
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, PhoneNumber = model.PhoneNumber, CreateTime = _creatTime, HeaderPic = _headerPic };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// 发送包含此链接的电子邮件
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
三、使用UserName进行登录
Models下AccountViewModels中,改更 public class LoginViewModel 模型,将EMAIL改为user
public class LoginViewModel
{
[Required]
[Display(Name = "用户名")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[Display(Name = "记住我?")]
public bool RememberMe { get; set; }
}
然后更改控制器的登录。AccountController
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
然后修改登录视图,将email改为username控件名
打开进行测试,注册和登录成功。

浙公网安备 33010602011771号