I have set some values with claims when the system logging.
public async Task<IActionResult> Login(string txtUserName, string txtPassword)
{
string username = txtUserName;
var claims = new List<Claim>
{
new Claim("username",username)
};
}
Now I wants to access the claim values in the Razor page. I try to access with flowing code line that @((ClaimsIdentity) User.Identity).GetSpecificClaim("username") in my Razor page but it's not works.
<div >
<a ><p>Website name</p></a>
<div >
<i ></i>
<a ><p>@((ClaimsIdentity)User.Identity).GetSpecificClaim("username")</p></a>
</div>
</div>
CodePudding user response:
You can add claims to HttpContext.User in backend like below:
public async Task<IActionResult> Login(string txtUserName, string txtPassword)
{
string username = txtUserName;
var claims = new List<Claim>
{
new Claim("username",username)
};
var appIdentity = new ClaimsIdentity(claims);
HttpContext.User.AddIdentity(appIdentity);
//....
}
Then get the claims in frontend like below:
@User.FindFirst("username").Value
