简体   繁体   中英

How to tell if user logged in via Facebook

Simple version of this question: how can I tell if a user is authenticated through Facebook after they are logged in with ASP.NET Identity?

VS2013, ASP.NET SPA Template, ASP.NET Identity 2.2.1:

I'd like to know if a user authenticated via Facebook. I was previously able to do this call:

ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

inside of my GetExternalLogin method. Now, though, it works the first time when called from GetExternalLogin (returning an object describing the facebook login) but upon returning form the postback to Facebook, it's called again from my GetUserInfo() method and it returns null. I dug into the code, and found the offending line:

if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
    return null;
}

(The value for both of these things is "LOCAL AUTHORITY" and null is returned.)

In the instance when it works correctly, providerKeyClaim.Issuer == "Facebook". Can anyone explain what this check is for, and why this condition is being met despite a Facebook login? I wish I knew when it last worked for me. I updated my version of ASP.NET Identity though I'm not sure if it worked after.

Update: Looks like during the call to GetExternalLogin, the User.Identity object was issued by Facebook. But for the subsequent GetUserInfo call, it's an entirely separate locally generated object, generated by the ASP.NET Identity components.

The ExternalLogin is stored in a separate cookie that is removed once a normal login happens (why you only see it for the first request). Identity v3 automatically adds a special claim which would allow you to see what the original login provider was. You can do something similar, by adding something like a LoginMethod claim to the user during that first request (inside the equivalent of the ExternalLoginCallback method that's in the default templates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM