在ASP.NET MVC 5中,默认情况下,登录和注册设置随附电子邮件和密码。我想改为使用用户名和密码。这里发布了许多类似的案例但是跟随它们并没有帮助。当我尝试注册时,收到错误消息“电子邮件不能为空”。似乎电子邮件的设置在某处仍然有效,我不知道在哪里。我对用户用户名而不是电子邮件所做的更改如下':
AccountViewModel
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);
}
帐户控制器将电子邮件更改为注册表中的用户名
<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>
Register.cshtml
<script language="JavaScript">
document.frm.submit();
</script>
在理解我缺少哪一步时,我会做同样的登录。
答案 0 :(得分:0)
这是猜测所以请善待。
在启动时配置AddIdentity。
这样的事情最有可能出现:
var appendHTML = function (el) {
var list = document.getElementById("events");
for (var i = 0; i < filteredDates.length; i++) {
var listItem = filteredDates[i].name;
listItem.className = "event-list";
listItem.setAttribute("onclick", "deleteEvent()");
list.appendChild(listItem);
}
};
var deleteEvent = function () {
var listItem = this.parentNode;
var ul = listItem.parentNode;
//Remove the parent list item from the ul
ul.removeChild(listItem);
return false;
};
将其更改为
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
这是基于使用EF和https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity/UserValidator.cs#L55
的假设