Search code examples
c#asp.net-corerazor-pages

Razor pages dereference of possible null reference on User.Identity.IsAuthenticated


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


Solution

  • 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();
    
    }