Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i have warning for Identity is null but i use ? and not fixed this

By using this code, I get Claims so I can use it, but it warns me that it is empty

Warning have :

Claim? ClaimsIdentity.FindFirst(string type) (+ 1 overload)

Retrieves the first claim with the specified claim type.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Returns:

The first matching claim or null if no match is found.

Exceptions:

ArgumentNullException
CS8602: Dereference of a possibly null reference

if (HttpContext.User.Identity is ClaimsIdentity identity)
{
    var IdUser = identity?.FindFirst(ClaimTypes.SerialNumber).Value;
}

>Solution :

FindFirst can return null, so you must also use the ? after it:

v = identity?.FindFirst(ClaimTypes.SerialNumber)?.Value;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading