public IAuthenticatedClient AuthenticateClient(IAuthenticationServiceSettings authenticationServiceSettings, NameValueCollection queryStringParameters) { if (authenticationServiceSettings == null) { throw new ArgumentNullException("authenticationServiceSettings"); } // First up - an authorization token. var authorizationCode = RetrieveAuthorizationCode(queryStringParameters, authenticationServiceSettings.State); // Get an Access Token. var oAuthAccessToken = RetrieveAccessToken(authorizationCode, authenticationServiceSettings.CallBackUri); // Grab the user information. var userInfo = RetrieveUserInfo(oAuthAccessToken.AccessToken); return(new AuthenticatedClient(Name.ToLowerInvariant()) { AccessToken = oAuthAccessToken.AccessToken, AccessTokenExpiresOn = DateTime.UtcNow.AddSeconds(oAuthAccessToken.ExpiresIn), UserInformation = new UserInformation { Id = userInfo.Id, Gender = string.IsNullOrEmpty(userInfo.Gender) ? GenderType.Unknown : GenderTypeHelpers.ToGenderType(userInfo.Gender), Name = userInfo.Name, Email = userInfo.Email, Locale = userInfo.Locale, Picture = userInfo.Picture, UserName = userInfo.GivenName } }); }
protected override UserInformation RetrieveUserInformation(AccessToken accessToken) { if (accessToken == null) { throw new ArgumentNullException("accessToken"); } if (string.IsNullOrEmpty(accessToken.PublicToken)) { throw new ArgumentException("accessToken.PublicToken"); } IRestResponse <UserInfoResult> response; try { var restRequest = new RestRequest("/v5.0/me"); restRequest.AddParameter(AccessTokenKey, accessToken.PublicToken); var restClient = RestClientFactory.CreateRestClient("https://apis.live.net"); TraceSource.TraceVerbose("Retrieving user information. Microsoft Live Endpoint: {0}", restClient.BuildUri(restRequest).AbsoluteUri); response = restClient.Execute <UserInfoResult>(restRequest); } catch (Exception exception) { var errorMessage = string.Format("Failed to retrieve any Me data from the Microsoft Live api. Error Messages: {0}", exception.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage, exception); } if (response == null || response.StatusCode != HttpStatusCode.OK) { var errorMessage = string.Format( "Failed to obtain some 'Me' data from the Microsoft Live api OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}. Error Message: {2}.", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription, response == null ? string.Empty : response.ErrorException == null ? "--no error exception--" : response.ErrorException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } // Lets check to make sure we have some bare minimum data. if (string.IsNullOrEmpty(response.Data.id)) { const string errorMessage = "We were unable to retrieve the User Id from Windows Live Api, the user may have denied the authorization."; TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } return(new UserInformation { Name = string.Format("{0} {1}", string.IsNullOrEmpty(response.Data.first_name) ? string.Empty : response.Data.first_name, string.IsNullOrEmpty(response.Data.last_name) ? string.Empty : response.Data.last_name).Trim(), Locale = response.Data.locale, UserName = response.Data.name, Id = response.Data.id, Email = response.Data.emails.Preferred, Gender = string.IsNullOrEmpty(response.Data.gender) ? GenderType.Unknown : GenderTypeHelpers.ToGenderType(response.Data.gender), }); }
protected override UserInformation RetrieveUserInformation(AccessToken accessToken) { if (accessToken == null) { throw new ArgumentNullException("accessToken"); } if (string.IsNullOrEmpty(accessToken.PublicToken)) { throw new ArgumentException("accessToken.PublicToken"); } IRestResponse <UserInfoResult> response; try { var restRequest = new RestRequest("/oauth2/v2/userinfo", Method.GET); restRequest.AddParameter(AccessTokenKey, accessToken.PublicToken); var restClient = RestClientFactory.CreateRestClient("https://www.googleapis.com"); TraceSource.TraceVerbose("Retrieving user information. Google Endpoint: {0}", restClient.BuildUri(restRequest).AbsoluteUri); response = restClient.Execute <UserInfoResult>(restRequest); } catch (Exception exception) { var errorMessage = string.Format("Failed to retrieve any UserInfo data from the Google Api. Error Messages: {0}", exception.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage, exception); } if (response == null || response.StatusCode != HttpStatusCode.OK) { var errorMessage = string.Format( "Failed to obtain some UserInfo data from the Google Api OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}. Error Message: {2}.", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription, response == null ? string.Empty : response.ErrorException == null ? "--no error exception--" : response.ErrorException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } // Lets check to make sure we have some bare minimum data. if (string.IsNullOrEmpty(response.Data.Id)) { const string errorMessage = "We were unable to retrieve the User Id from Google API, the user may have denied the authorization."; TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } return(new UserInformation { Id = response.Data.Id, Gender = string.IsNullOrEmpty(response.Data.Gender) ? GenderType.Unknown : GenderTypeHelpers.ToGenderType(response.Data.Gender), Name = response.Data.Name, Email = response.Data.Email, Locale = response.Data.Locale, Picture = response.Data.Picture, UserName = response.Data.GivenName }); }
protected override UserInformation RetrieveUserInformation(AccessToken accessToken) { if (accessToken == null) { throw new ArgumentNullException("accessToken"); } if (string.IsNullOrEmpty(accessToken.PublicToken)) { throw new ArgumentException("accessToken.PublicToken"); } IRestResponse <MeResult> response; try { var restRequest = new RestRequest("v2.5/me"); restRequest.AddParameter("access_token", accessToken.PublicToken); restRequest.AddParameter("fields", "name,email,first_name,last_name,locale,gender,link"); var restClient = RestClientFactory.CreateRestClient(BaseUrl); TraceSource.TraceVerbose("Retrieving user information. Facebook Endpoint: {0}", restClient.BuildUri(restRequest).AbsoluteUri); response = restClient.Execute <MeResult>(restRequest); } catch (Exception exception) { var authenticationException = new AuthenticationException("Failed to retrieve any Me data from the Facebook Api.", exception); var errorMessage = authenticationException.RecursiveErrorMessages(); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage, exception); } if (response == null || response.StatusCode != HttpStatusCode.OK || response.Data == null) { var errorMessage = string.Format( "Failed to obtain some 'Me' data from the Facebook api OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}. Error Message: {2}.", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription, response == null ? string.Empty : response.ErrorException == null ? "--no error exception--" : response.ErrorException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } var id = response.Data.Id < 0 ? 0 : response.Data.Id; var name = (string.IsNullOrEmpty(response.Data.FirstName) ? string.Empty : response.Data.FirstName) + " " + (string.IsNullOrEmpty(response.Data.LastName) ? string.Empty : response.Data.LastName).Trim(); var userInformation = new UserInformation { Id = id.ToString(), Name = name, Email = response.Data.Email, Locale = response.Data.Locale, UserName = response.Data.Username, Gender = string.IsNullOrWhiteSpace(response.Data.Gender) ? GenderType.Unknown : GenderTypeHelpers.ToGenderType(response.Data.Gender), Picture = string.Format("https://graph.facebook.com/{0}/picture", id) }; return(userInformation); }