Displaying welcome message at navbar!

Asked

Viewed 118 times

1

I’m doing an app on MVC Asp.net. When the user logs in I would like you to display the welcome message bringing the user name, however you are bringing the email, as I change this?

My action is that way:

@Html.ActionLink("Olá " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
  • Some answers to OS questions indicate using User.Identity.Name; has tested?

  • Thus the error indicating that there is no argument provided that corresponds to the required parameter.

  • Using Windows Authentication? Or what form of user authentication?

1 answer

3


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. =)

  • perfect guy, that’s right. Thank you so much!!

  • There is only one problem, I do the registration and everything ok, so when I leave I try to login again invalid!

  • 1

    @Wpfan there you have to see how the login is being done, I believe by email, take a look at AccountController and try to understand what she’s doing in the login.

  • I got it, I changed it here so that I requested the username and password instead of email and password, so everything worked out... really worth!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.