How to get an application inside Asp.Net Core Razor View

I did this in my rc1 project, for example:

User.Claims.ElementAt(#).Value

But after I switched to rtm, it no longer worked. When I debug the Razor view, the object looks the same, but User.Claims is just empty. Think about what could be the reason.

+4
source share
1 answer

Assuming that you have a complaint against the current leader. In the "Razor" view:

@((ClaimsIdentity) User.Identity)

This will give you access to the current user's ClaimsIdentity. In an effort to keep your requirements clean, you can create an extension method for claiming.

public static string GetSpecificClaim(this ClaimsIdentity claimsIdentity, string claimType)
{
    var claim = claimsIdentity.Claims.FirstOrDefault(x => x.Type == claimType);

    return (claim != null) ? claim.Value : string.Empty;
}

Then you can simply access any application you want:

@((ClaimsIdentity) User.Identity).GetSpecificClaim("someclaimtype")

Hope this helps.

: MVC 5

+7

Source: https://habr.com/ru/post/1652344/


All Articles