public async Task <IHttpActionResult> SignInLinkedin(SignInLinkedinModel model) { this.CheckModelStateIsValid(); string token; var userSession = this.GetCurrentUserSession(); if (userSession == null) { var externalUserDetails = await LinkedinUserDetailsProvider.GetUserDetails(model, Request.GetOwinContext().Request.CallCancelled); userSession = _userSessionBusinessLogic.CreateSessionForExternalUser(externalUserDetails); token = JwtHelper.Create(userSession); } else { Log.InfoFormat("User '{0}' is already signed-in. Reusing existing session.", userSession.UserId); // return the bearer token received token = Request.Headers.Authorization.Parameter; } var result = new SignInResultModel { Token = token }; return(Ok(result)); }
public async Task <IActionResult> SignInLinkedin([FromBody] SignInLinkedinModel model) { this.CheckModelStateIsValid(); string token; var userSession = await this.GetCurrentUserSession(); if (userSession == null) { var externalUserDetails = await LinkedinUserDetailsProvider.GetUserDetails(HttpContext.RequestServices, model, Request.HttpContext.RequestAborted); userSession = await _userSessionBusinessLogic.CreateSessionForExternalUser(externalUserDetails); token = _jwtSecurityTokenFactory.Create(userSession); } else { _logger.LogInformation("User '{0}' is already signed-in. Reusing existing session.", userSession.UserId); // return the bearer token received //TODO: token = Request.Headers. Authorization.Parameter; token = "TODO"; } var result = new SignInResultModel { Token = token }; return(Ok(result)); }
public static async Task <ExternalUserDetails> GetUserDetails(SignInLinkedinModel model, CancellationToken cancellationToken) { if (model == null) { return(null); } HttpClient httpClient = null; try { httpClient = CreateHttpClient(); //1. get the access token var responseMessage = await httpClient.PostAsync(AccessTokenUrl, new FormUrlEncodedContent(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "authorization_code"), new KeyValuePair <string, string>("code", model.Code), new KeyValuePair <string, string>("redirect_uri", model.RedirectUri), new KeyValuePair <string, string>("client_id", ApplicationSettings.LinkedinClientId), new KeyValuePair <string, string>("client_secret", ApplicationSettings.LinkedinClientSecret) }), cancellationToken); responseMessage.EnsureSuccessStatusCode(); var jObject = JObject.Parse(await responseMessage.Content.ReadAsStringAsync()); var accessToken = jObject.GetStringValue("access_token"); //2. get user info var request = new HttpRequestMessage(HttpMethod.Get, UserInfoBaseUrl + "?format=json&oauth2_access_token=" + Uri.EscapeDataString(accessToken)); responseMessage = await httpClient.SendAsync(request, cancellationToken); responseMessage.EnsureSuccessStatusCode(); var responseContent = await responseMessage.Content.ReadAsStringAsync(); jObject = JObject.Parse(responseContent); var result = new ExternalUserDetails { UserType = UserType.ExternalLinkedin, Id = jObject.GetStringValue("id"), Email = jObject.GetStringValue("emailAddress"), FirstName = jObject.GetStringValue("firstName"), LastName = jObject.GetStringValue("lastName"), DisplayName = jObject.GetStringValue("formattedName"), Description = jObject.GetStringValue("headline"), PictureUrl = GetPictureUrl(jObject) }; return(result); } catch (Exception e) { Log.Error("Could not fetch Linkedin user details.", e); return(null); } finally { if (httpClient != null) { httpClient.Dispose(); } } }