0

enter image description here

I have this if statement in the cshtml of a page. For some reason or another it keeps giving me that the User.Identity.IsAuthenticated is dereference of possible null reference and when the program starts it doesn't consider the statement.How can it be fixed?

this is after implementing your solution

Addition: the error occurs on post

Stoyan
  • 23
  • 4

3 Answers3

4

Use:

@if (User.Identity?.IsAuthenticated == true)
{
}

Check Nullable contexts and Null-conditional operators.

Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
-1

I also get Dereference of possible null reference when using User.Identity.IsAuthenticated, but it's not ignored, it works, have you verified this page?

Other than that, maybe you can use Request.IsAuthenticated instead.

Chen
  • 4,499
  • 1
  • 2
  • 9
-1

I fixed the error. I called onGet method inside the onPost method before returning the page example:

public void onGet()
{
//code
}
public void onPost()
{

//code 

onGet();

return Page();

}
Stoyan
  • 23
  • 4