Access token after logged in
I am trying to build a Facebook login function with native Facebook sdk. I call the api with Facebook token and user id to verify and login identity server account but I cannot get the token after logged in.
Here is my step for verification:
-
Get Facebook email using Facebook access token and user id;
-
Verify email with Database;
-
Sign in Identity server;
Here is my code for verification
[HttpPost]
[AllowAnonymous]
public async Task<string> GetTokenForIOS(FacebookUserModel model)
{
model.email = await GetEmail(model);
var user = await _userManager.FindByNameAsync(model.email);
if (user != null)
{
// sign in identityServer
await _signInManager.SignInAsync(user, isPersistent: false);
var externalAccessToken = await _userManager.GetAuthenticationTokenAsync(user, "IDV4", "access_token");
string token = await GetAccessToken();
return token;
} else {
FacebookUserInfoModel infoModel = await GetFacebookUserInfo(model);
...
// sign in Identity Account after registration
// return token;
}
return "";
}
Here is my code for getting token
public async Task<string> GetAccessToken()
{
// discover endpoints from metadata
var disco = await DiscoveryClient.GetAsync("https://localhost:44355");
// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "client.ios");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api");
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return "";
}
else
{
Console.WriteLine(tokenResponse.Json);
JToken jObject = tokenResponse.Json;
return tokenResponse.Json.ToString();
}
}
Here is my client config:
new Client
{
ClientId = "client.ios",
RequireClientSecret = false,
ClientName = "iOS app client",
AllowedGrantTypes = GrantTypes.Code,
RequirePkce = true,
//AllowAccessTokensViaBrowser = true,
RequireConsent = false,
RedirectUris = { "net.openid.appauthdemo:/oauth2redirect" },
PostLogoutRedirectUris = { "net.openid.appauthdemo:/oauth2redirect" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.Phone,
"api"
},
AllowOfflineAccess = true
}
[2017-07-14 UPDATED] Here is my code for calling Facebook API:
public async Task<FacebookUserInfoModel> GetFacebookUserInfoAsync(FacebookUserModel model)
{
TaskCompletionSource<IRestResponse> taskCompletationSource = new TaskCompletionSource<IRestResponse>();
RestClient client = new RestClient("https://graph.facebook.com/v2.4/me?access_token="
+ model.access_token + "&fields=email,name,first_name,last_name,gender,timezone,age_range,link,locale");
var request = new RestRequest(Method.GET);
request.AddHeader("Accept", "application/json");
client.ExecuteAsync(request, r => taskCompletationSource.SetResult(r));
RestResponse response = (RestResponse)(await taskCompletationSource.Task);
if (response.StatusCode == HttpStatusCode.OK && response.ContentType.Contains("application/json"))
{
if (response.Content != null)
{
var info = JsonConvert.DeserializeObject<FacebookUserInfoModel>(response.Content);
return info;
}
}
return null;
}
The token response for access_token always is null. How can I get or generate the user access token with the user information for response or it's impossible to get token here?
I'm confused -- how does the access token for the FaceBook API have anything to do with IdentityServer?
In these code, it only show my to try to login IdentityServer and get the access_token. Normally there are some checking for the user verification and login/register action. In the GetMail method, I will use the FaceBook access_token and email to get FaceBook user's email address. Then I will verify the email is existed in DB or not. If the email is existed then use the userManager sign in method. If not then to use the FaceBook user information to register a new account via userManager sign up method.
The major reason is I want to use native FB sdk to do FB login and then sign in IdentityServer to get tokenResponse to login my back-end service account through AppAuth. In your view, is it possible?
did you put "email" in the "scope"? I know on Google you need to add to registration to get email.
I passed the FB access_token and userId as model to be parameter. I can call the FB api https://.../me?fields=email&access_token in the GetMail method to get the user email for verification. If registration is needed then I wall add more fields in the api to get more information in other method and then register a new IdentityServer user. In this case, I want to get the user token response after logged in IdentityServer . However my way is not using the Facebook login for asp.net core, I cannot use the UserManager to get the token response.
The below steps are my Facebook Login flow:
In Mobile:
-
iOS native Facebook login;
-
retrieve Facebook response then call api with Facebook access_token and userID
In IdentityServer:
-
retrieve Facebook access_token and userID then call Facebook user email via Facebook API with access_token;
-
retrieve Facebook user email then verify the email.
-
if email is existed, sign in the email's owner account of IdentityServer;
-
get token response then return to mobile app;
In Mobile:
-
retrieve the IdentityServer token response and then save to stateAuth of AppAuth;
-
use AppAuth login back-end server;
This flow only shows the normal Facebook login successfully. Some checking such as local login/Facebook login, login/registration and get Facebook information is success or not are not considering.
All set on this issue? Can we close?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.