If you are using the template ASP.NET MVC
with Identity
, by default it already saves the Username with the email provided in the method Register
, look at you:
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
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);
}
return View(model);
}
You can modify this behavior... like?
Change your class RegisterViewModel
:
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required,Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
And in the View
Register.cshtml
add the field that will receive the Username
<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>
Once done, change what the Username
is receiving in the method Register
in AccountController
var user = new ApplicationUser { UserName = model.Username, Email = model.Email };
And ready, your Username
will have the value informed at the time of Registration. =)
Some answers to OS questions indicate using
User.Identity.Name
; has tested?– rLinhares
Thus the error indicating that there is no argument provided that corresponds to the required parameter.
– WPfan
Using Windows Authentication? Or what form of user authentication?
– Felipe Grossi