get a null exception with a variable in a list of claims RRS feed

  • Question

  • Hello, I have the code below where I create a list of claims and add some values:

    string email = id_token.GetValue("unique_name").ToString();
    string name = id_token.GetValue("given_name").ToString();
    DataSet ds; 
    List<Claim> claims = new List<Claim>()
        new Claim(ClaimTypes.Name, name),
        new Claim(ClaimTypes.Email, email)
    string roles = "select name from Roles_Table";
    ds = GetDataSet(roles);
    if (ds.Tables.Count > 0)
        foreach (var row in ds.Tables(0).Rows)
            claims.Add(new Claim(ClaimTypes.Role, row("name")));

    When I want to use it like this:

    ClaimsPrincipal cp = ClaimsPrincipal.Current;
    var email = cp.FindFirst(ClaimTypes.Email).Value

    It says email is null and I don't know why. I also tried :

    var email =cp.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value;

    But got the same issue. However, the other values of the list are not null. Need help please.

    Thursday, November 15, 2018 9:45 PM

All replies

  • Greetings kstMan.

    I don't think you are giving us enough info. What is ClaimsPrinciple, and where are you setting ClaimsPrinciple.Current?

    Thursday, November 15, 2018 10:39 PM
  • This looks like a web application. It looks like you're trying to create a custom user claim to associate for authentication. This type of question would need to be posted in the ASP.NET forums. The reason for this is because you have to ensure that list of claims you're setting up gets properly associated with the user's request otherwise it won't survive the roundtrip. So when you later query the current thread's principal you won't be getting back the claims you added. Hence the error.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, November 16, 2018 3:35 AM
  • Hi, kstMan,

    Thank you for posting here.

    For your question, Please provide the following information:

    1. Definition of id_token 
    2. Method of GetValue and GetDataSet
    3. Database content

    We could not test the code, please provide more details.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 16, 2018 5:52 AM