@manhng

Welcome to my blog!

Claims-based Authentication

June 3, 2019 18:25

Claims (edit)

_Header.cshtml 

@using Microsoft.AspNet.Identity
@using System.Security.Claims
@using WebApp.Mvc.Helpers

@{
var Fullname = string.Empty;
var claimsIdentity = User.Identity as System.Security.Claims.ClaimsIdentity;

if (claimsIdentity != null)
{
var userData = claimsIdentity.FindFirst(System.Security.Claims.ClaimTypes.UserData);
if (userData != null)
{
var userBase = JsonUtils.DeserializeObject<UserBase>(userData.Value);
if (userBase != null)
{
Fullname = userBase.Fullname;
}
}
}
}

https://stackoverflow.com/questions/39125347/how-to-get-claim-inside-asp-net-core-razor-view

https://stackoverflow.com/questions/19524050/get-a-user-claim-in-asp-net-mvc-4-razor-view/19526229

    @{
        var claimsIdentity = User.Identity as System.Security.Claims.ClaimsIdentity;

        if (claimsIdentity != null)
        {
            var c = claimsIdentity.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);

            if (c != null)
            {
             <p>
                 @c.Type.ToString(); 
                 @c.Value.ToString();
             </p>   
            }
        }
    }

Categories

Recent posts