在ASP.NET MVC 5中,默认情况下,登录和注册设置附带电子邮件和密码。我想改为使用用户名和密码。这里发布了一些类似的病例,但是跟着他们并没有帮助。当我尝试注册时,出现错误消息“电子邮件不能为空”。看起来电子邮件的设置仍然有效,不知道在哪里。改变我对用户的用户名,而不是电子邮件做出如下:更改用户从电子邮件登录到用户名
AccountViewModel
//Removed Email and added username for RegisterViewModel
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
}
的AccountController改变电子邮件的用户名在注册
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{ //changed email to username
var user = new ApplicationUser { UserName = model.Username};
//var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
Register.cshtml
<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>
在了解我失踪的步骤后,我会做同样的登录。
跟踪它在调试器中看到的假设错误来自哪里。验证通常至少有3个层次(例如View,Model,Entity)。 –
我对已更改的文件使用了断点。什么都没有出现。 IT不是一个例外,而是显示一个字符串,因此异常被捕获到某处。确切的字符串是“电子邮件不能为空或空”。试图使用find进行搜索,该字符串不会显示在公共文件上。我猜我得按档案去档案。 – kar
UserManager仍然在寻找一封电子邮件,因为您评论它是有道理的。要求电子邮件注册但不能登录。 –