2015-11-18 135 views
1

在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> 

在了解我失踪的步骤后,我会做同样的登录。

+0

跟踪它在调试器中看到的假设错误来自哪里。验证通常至少有3个层次(例如View,Model,Entity)。 –

+0

我对已更改的文件使用了断点。什么都没有出现。 IT不是一个例外,而是显示一个字符串,因此异常被捕获到某处。确切的字符串是“电子邮件不能为空或空”。试图使用find进行搜索,该字符串不会显示在公共文件上。我猜我得按档案去档案。 – kar

+0

UserManager仍然在寻找一封电子邮件,因为您评论它是有道理的。要求电子邮件注册但不能登录。 –

回答

0

这是一个猜测,所以请亲切。

在您的启动中,您配置AddIdentity。

像这样的东西是最有可能的原因:

// Add Identity services to the services container. 
services.AddIdentity<ApplicationUser, IdentityRole>() 
    .AddEntityFrameworkStores<ApplicationDbContext>() 
    .AddDefaultTokenProviders(); 

将其更改为

// Add Identity services to the services container. 
    services.AddIdentity<ApplicationUser, IdentityRole>(options => { 
     options.User.RequireUniqueEmail = false; }) 
    .AddEntityFrameworkStores<ApplicationDbContext>() 
    .AddDefaultTokenProviders(); 

这是基于使用EF和https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity/UserValidator.cs#L55

+0

这个文件在哪里?你提到启动在App_Start/Startup.Auth.cs? – kar

相关问题