1

I am trying to update the user name in the navbar (using ASP.NET, MVC, C#) after a user has changed his or here user name when signed in. So the user name is shown, but when it's changed the old user name is still being displayed in the navbar until the user loges out and in again.

Dose any one know how to display the new user name in the navbar in a nice way?

This is a short version of how the navbar looks like:

<nav class="navbar navbar-default navbar-fixed-top">
@if (Request.IsAuthenticated)
{
    @Html.AntiForgeryToken()
    <a href='@Url.Action("Index", "Manage")'>@User.Identity.GetUserName()</a>
}
</nav>

And below is the short version for how the new user name / e-mail is updated:

aspUser.UserName = model.Email;
aspUser.Email = model.Email;
var result = await UserManager.UpdateAsync(aspUser);

if (result.Succeeded)
// Want to update the navbar, maybe here?

(e-mail and user name is the same thing in this application)

5
  • Possible duplicate... Commented Jul 24, 2017 at 14:28
  • 1
    As you said you need to logout and login user with this request so run await SignInAsync(user,true); to programatically sign in new username. Commented Jul 24, 2017 at 14:32
  • @PeterSchneider Similar but not the same. I am able to save the new user name and e-mail. However, my problem lies in showing the new user name in the navbar. Right now the old user name is still being displayed in the navbar. Commented Jul 24, 2017 at 14:35
  • @FarrukhSubhani, Thank you so much! It worked. I used: await SignInManager.SignInAsync(aspUser, true, true);. Commented Jul 24, 2017 at 14:42
  • Can you mark it as an answer please. Commented Jul 25, 2017 at 15:20

1 Answer 1

2

In order to sign in user automatically after change of username you need to run SingInAsync method as per my comments on your question. await SignInManager.SignInAsync(aspUser, true, true);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.