asp.netasp.net-coreasp.net-identity.net-9.0

How can a user's selected authentication method be retrieved?


I want to retrieve the authentication method of a logged-in user, e.g. something like "by username/password", or for oauth cases "via facebook" or "via google".

After some research I found this piece of code that should retrieve the authentication method out of a user's claims:

HttpContext.User.FindFirstValue(ClaimTypes.AuthenticationMethod);

However, this always returns null for logged-in users. If I inspect the User object I can see different claims. For a user that authenticated with username/password there is a claim of type "amr" with a value of "pwd" that looks like what I'm looking for. However, the constant ClaimTypes.AuthenticationMethod has the value "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod" (which is of course the reason I got null as return in that case).

In the case of users logging in via oauth like Facebook or Google there is not even a claim that seems to convey authentication method information. The only claims I have for those type of users is "name identifier", "name", "email address" and "security stamp".

So, how do I retrieve the information I'm looking for?

Many thanks in advance for your help!


Solution

  • The value of ClaimTypes.AuthenticationMethod will be appended by SignInManager ,see the source codes here.

    Follow the steps in this document(there're also documents for Facebook and Google ),after you've login,you would see the value of ClaimTypes.AuthenticationMethod:

    enter image description here