Find Normalizedusername of Identity . NET core on the Azor pages

Asked

Viewed 24 times

1

I need to display the user name on topbar. Razor by default provides a @User object as default but providing little user information. It has the name property but brings the username instead of the user name. It would be possible to somehow search the Normalizedusername by View?

1 answer

0


You have the option to inject Usermanager into your controller, and capture user information using _userManager.GetUserAsync(User); and sending to your view on an object or filling your Viewmodel with that information and capturing in your View.

private UserManager<User> _userManager;       
public SeuController(UserManager<User> userManager) {
_userManager = userManager;
    }

public IActionResult Get() {
var user = await _userManager.GetUserAsync(User);
// user.NormalizedUserName <-- passe isso pra sua view

}

The other option is to inject the UserManager<User> userManager with the @inject right in the view.

@inject UserManager<User> userManager
<div>
@{

var user = await _userManager.GetUserAsync(User);

}
<span>
@user.NormalizedUserName 
</span>
<div>

I would recommend using the first option in most cases.

Browser other questions tagged

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