46
1
using System;
2
using System.Linq;
3
using System.Collections.Generic;
4
using System.Security.Claims;
5
6
public class Program
7
{
8
public static void Main()
9
{
10
var claims = new List<Claim>
11
{
12
new Claim(ClaimTypes.Name,"test name"),
13
new Claim("Token","token123"),
14
new Claim("Number","3")
15
//new Claim("dummy","dummy string")
16
};
17
var ci = new ClaimsIdentity(claims);
18
19
//most seen way, throw error if not exists
20
var name = ci.Claims.First(x => x.Type == ClaimTypes.Name).Value;
21
var token = ci.Claims.First(x => x.Type == "Token").Value;
22
//using FirstOrDefault() only, throw error if not exists
23
//var dunno = ci.Claims.FirstOrDefault(x => x.Type == "dunno").Value;
24
Cached Result