Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# .net JwtSecurityTokenHandler jwttoken claims to object

public static ClaimsPrincipal ValidateToken(string jwtToken)
    {
        IdentityModelEventSource.ShowPII = true;

        SecurityToken validatedToken;
        TokenValidationParameters validationParameters = new TokenValidationParameters();

        validationParameters.ValidateLifetime = true;

        validationParameters.ValidAudience = _audience.ToLower();
        validationParameters.ValidIssuer = _issuer.ToLower();
        validationParameters.IssuerSigningKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSettings.Secret));

        ClaimsPrincipal principal = new JwtSecurityTokenHandler().ValidateToken(jwtToken, validationParameters, out validatedToken);


        return principal;
    }
Comment

C# .net JwtSecurityTokenHandler jwttoken claims to object

var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
    IEnumerable<Claim> claims = identity.Claims; 
    // or
    identity.FindFirst("ClaimName").Value;

}
Comment

C# .net JwtSecurityTokenHandler jwttoken claims to object

HttpContext.User.Identity.MethodName();
Comment

C# .net JwtSecurityTokenHandler jwttoken claims to object

You should be able to retrieve a claims like this within your controller

var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
    IEnumerable<Claim> claims = identity.Claims; 
    // or
    identity.FindFirst("ClaimName").Value;

}
If you wanted, you could write extension methods for the IPrincipal interface and retrieve claims using the code above, then retrieve them using (for example)

HttpContext.User.Identity.MethodName();
Comment

C# .net JwtSecurityTokenHandler jwttoken claims to object

// Cast to ClaimsIdentity.
var identity = HttpContext.User.Identity as ClaimsIdentity;

// Gets list of claims.
IEnumerable<Claim> claim = identity.Claims; 

// Gets name from claims. Generally it's an email address.
var usernameClaim = claim
    .Where(x => x.Type == ClaimTypes.Name)
    .FirstOrDefault();

// Finds user.
var userName = await _userManager
    .FindByNameAsync(usernameClaim.Value);

if (userName == null)
{
    return BadRequest();
}

// The rest of your code goes here...
Comment

C# .net JwtSecurityTokenHandler jwttoken claims to object

In any controller from net core 2 that has gone through the authorize with the JwtBearerDefaults scheme, you can use:

 [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
 public ActionResult Index()
    {
        var user = User.FindFirst("Name").Value;
        //or if u want the list of claims
        var claims = User.Claims;

        return View();
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# wait without GUI blocks 
Csharp :: c# lernen kostenlos 
Csharp :: windows forms tablelayoutpanel scroll 
Csharp :: c# force arguments to be keywords 
Csharp :: regex ip rage detect c# 
Csharp :: vb.net how insert event inside an event 
Csharp :: extension method c# 
Csharp :: how to make infinite loop in c# 
Csharp :: set main camera unity 
Csharp :: unity int to bool 
Csharp :: list<string,string c# 
Csharp :: c# is string nullable 
Csharp :: c# close all threads application exit 
Csharp :: c# datatable current row 
Csharp :: it solutions 
Csharp :: c# get regedit value 
Csharp :: unity ik nothing is happening 
Csharp :: getawaiter and no extension method 
Csharp :: random number between 1 and 100 c# 
Html :: href do nothing 
Html :: how to open link in new tab 
Html :: center text v-card 
Html :: Add Random Image from Web in html 
Html :: c# strip html tags 
Html :: feather icon cdn 
Html :: html ß 
Html :: non breaking space html 
Html :: favicon in html document 
Html :: html space between characters 
Html :: download ebook free 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =