Search code examples
asp.net-mvcasp.net-coreasp.net-identityidentity

SignInAsync verifies authenticated cookie?


This document says:

We are invoking PasswordSignInAsyncto verify and issue an authentication cookie

Does this method really verify the authenticated cookie? What does it when it does verify, how can i fetch the result of said Verification?

In other words: What is the best method to know that a user is signed in in identity?


Solution

  • ...verify the authenticated cookie

    That's not how that sentence is meant to be read, I would assume.

    PasswordSignInAsync verifies the credentials that are passed to it, and if they are valid, it creates the authentication cookie and adds it to the response.

    ...method to know that a user is signed in in identity

    The authentication cookie is used to recreate the Identity and Principal objects on each request, so the simple answer to your question is

    User.Identity.IsAuthenticated // this is a Boolean value
    

    If you want to stick with the SignInManager, use IsSignedIn(), which would be called like this:

    // Assuming you have an instance of SignInManager called _signInManager
    _signInManager.IsSignedIn(User);
    

    You probably want to review something like How get current user in asp.net core?