Refresh user name in navbar after username has changed (Using Asp.Net, MVC and C#)

若如初见. 提交于 2019-12-23 03:11:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!