Use Username instead of Email for identity in Asp.net mvc5

前端 未结 4 1263
南旧
南旧 2020-12-02 15:49

Whenever I create a new application with visual studio 2013 express for web and using the individual accounts authentication and i hit the register button I notice that it i

4条回答
  •  星月不相逢
    2020-12-02 16:19

    The linked question in the accepted answer descibes how to use Email instead of UserName, OP wanted the UserName instead of email which is what I was looking for in Identity 2.0 in an MVC project.

    In case anyone else gets here from a google search it is actually very easy to do this. If you look at the register post action it is simply setting the UserName to the Email address. So.........

    Add UserName to the RegisterViewModel and add it to the register view.

    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "Username or email" })

    In the Register Post Action on the AccountController set the UserName to the ViewModel's UserName and the Email to the ViewModel's Email.

    var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
    

提交回复
热议问题