简体   繁体   中英

how to retrieve facebook user info ASP.NET WEB API 2

I want to register a user, through an external provider (like facebook), in order to get the information I need, I configure FacebookProvider as follows

var options = new FacebookAuthenticationOptions {
    AppId = "***",
    AppSecret = "***",
    Scope = { "email" },
    Provider = new FacebookAuthenticationProvider {
        OnAuthenticated = (context) => {
            foreach (var x in context.User)
            {
                var claimType = string.Format("urn:facebook:{0}", x.Key);
                string claimValue = x.Value.ToString();
                if (!context.Identity.HasClaim(claimType, claimValue))
                context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));
            }

            return Task.FromResult(0);
        }
    }
};

options.Fields.Add("id"); 
options.Fields.Add("name"); 
options.Fields.Add("email");

options.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalBearer;

app.UseFacebookAuthentication(options);

in the OnAuthenticated while debugging I see all the requested fields but when I call RegisterExternal from postman as follow pic

RegisterExternal call postman

GetExternalLoginInfoAsync returns null

var info = await Authentication.GetExternalLoginInfoAsync();
if (info == null)
{
    return InternalServerError();
}

so how to retrieve query fields such as email? I think all the necessary information is stored in the cookies, but how do I transfer them to the server and extract Identity instance?

all nuget packages have been updated to the latests versions

ps I plan to work with the API from the iOS app

I found solution.

Changed ExternalLoginData class as follows

private class ExternalLoginData
{
    ...
    // here added new field
    public IList<Claim> Claims { get; private set; }

    public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
    {
        ...
        return new ExternalLoginData
        {
            ...
            // here added claims setting
            Claims = identity.Claims.ToList()
        };
    }
}

Changed ExternalLogin callback as follows

public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
    ...
    if (hasRegistered)
    {
        ...
    }
    else
    {
        // here replaced getting claims by Claims field
        IEnumerable<Claim> claims = externalLogin.Claims;
        //IEnumerable<Claim> claims = externalLogin.GetClaims();
        ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
        Authentication.SignIn(identity);

    }

    return Ok();
}

As a result we receive a bearer token. Extracting Identity from it we receive earlier saved claims.

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