I am building a prediction website on ASP.NET core in Visual Studio and of course I need a "score" variable for each user. That's why I added a "score" column to the built-in IdentityUser table by creating a new class with IdentityUser as base class. The only thing I added there is a property called Score.
public class UserIdentity : IdentityUser<Guid>
{
public int Score { get; set; }
}
Now I want to display the score in the navbar along with the username of the current user, but I get the error that Score does not exist in the same namespace as the username.
HTML code from my _Layout file (for the navigation bar):
@if (signInManager.IsSignedIn(User))
{
<li class="nav-item">
<form method="post" asp-action="logout" asp-controller="account">
<button type="submit" class="nav-link btn btn-link py-0" style="width:auto">Uitloggen @User.Identity.Name <br /> @Convert.ToString(User.Identity.Score) punten</button>
</form>
</li>
}
Thanks in advance!
The easiest way you could do this by using UserManager
, your login partial should look like this
@inject UserManager<UserIdentity> UserManager
@if (signInManager.IsSignedIn(User))
{
var user = await UserManager.FindByNameAsync(User.Identity.Name);
<li class="nav-item">
<form method="post" asp-action="logout" asp-controller="account">
<button type="submit" class="nav-link btn btn-link py-0" style="width:auto">Uitloggen @User.Identity.Name <br /> @user.Score punten</button>
</form>
</li>
}
For your _Layout file you could implement this by following way
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebApplication5</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css"
/>
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@if (signInManager.IsSignedIn(User))
{
var user = await UserManager.FindByNameAsync(User.Identity.Name);
<li class="nav-item">
<form method="post" asp-action="logout" asp-controller="account">
<button type="submit" class="nav-link btn btn-link py-0" style="width:auto">Uitloggen @User.Identity.Name <br /> @user.Score punten</button>
</form>
</li>
}
Another way you could extend you User Identity based on claim principle. For More Please follow this link