public async Task <IHttpActionResult> ObtainLocalAccessToken(string provider, string externalAccessToken) { if (string.IsNullOrWhiteSpace(provider) || string.IsNullOrWhiteSpace(externalAccessToken)) { return(BadRequest("Provider or external access token is not sent")); } // Verify that the access token supplied is valid ParsedExternalAccessToken verifiedAccessToken = await VerifyExternalAccessToken(provider, externalAccessToken); if (verifiedAccessToken == null) { return(BadRequest("Invalid Provider or External Access Token")); } // Find the user in our repository IUser user = repository.GetUser(verifiedAccessToken.user_id, provider); if (user == null) { // Register now user = accountBusiness.CreateNewUser( new User { AuthenticationUserId = verifiedAccessToken.user_id, AuthenticationProvider = provider, UserName = verifiedAccessToken.username }); } //generate access token response var accessTokenResponse = GenerateLocalAccessTokenResponse(user); // Return success with the bearer token for authorized access return(Ok(new { token = accessTokenResponse, profile = user })); }