问题
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)
回答1:
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);
来源:https://stackoverflow.com/questions/45283124/refresh-user-name-in-navbar-after-username-has-changed-using-asp-net-mvc-and-c