public static void RegisterAuth() { if (OpenAuth.AuthenticationClients.GetByProviderName("linkedin") == null) { var client = new LinkedInOAuth2Client("86h0tsh0fansqk", "GzIEj706ElSYEWT9"); OpenAuth.AuthenticationClients.Add("linkedin", () => client); } }
public IAuthenticationClient Build(ProviderConfigurationViewModel providerConfiguration) { string ClientId = providerConfiguration.ProviderIdKey; string ClientSecret = providerConfiguration.ProviderSecret; var client = new LinkedInOAuth2Client(ClientId, ClientSecret); return(client); }
public ActionResult ExternalLoginCallback(string returnUrl) { string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest(); if (ProviderName == null || ProviderName == "") { NameValueCollection nvs = Request.QueryString; if (nvs.Count > 0) { if (nvs["state"] != null) { NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]); if (provideritem["__provider__"] != null) { ProviderName = provideritem["__provider__"]; } } } } LinkedInOAuth2Client.RewriteRequest(); var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }); var authResult = OpenAuth.VerifyAuthentication(redirectUrl); string providerDisplayName = OpenAuth.GetProviderDisplayName(ProviderName); if (!authResult.IsSuccessful) { return(Redirect(Url.Action("Index", "Home"))); } else { //Get provider user details string providerUserId = authResult.ProviderUserId; string providerUserName = authResult.UserName; string firstName = null; string lastName = null; string accessToken = null; string email = null; if (email == null && authResult.ExtraData.ContainsKey("email-address")) { email = authResult.ExtraData["email-address"]; } if (firstName == null && authResult.ExtraData.ContainsKey("first-name")) { firstName = authResult.ExtraData["first-name"]; } if (lastName == null && authResult.ExtraData.ContainsKey("last-name")) { lastName = authResult.ExtraData["last-name"]; } if (accessToken == null && authResult.ExtraData.ContainsKey("accesstoken")) { accessToken = authResult.ExtraData["accesstoken"]; } var userInfo = new List <object>(); userInfo.Add(new { ProviderDisplayName = providerDisplayName, ProviderUserId = providerUserId, FirstName = firstName, LastName = lastName, Email = email, AccessToken = accessToken }); return(RedirectToAction("RedirectToClient")); } }
public IHttpActionResult ExternalLoginCallback(string code, string state) { string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest(); string username = ""; string returnURI = ""; // For future user when we integrate with other oauth2 applications if (ProviderName == null || ProviderName == "") { var nvs = Request.GetQueryNameValuePairs(); string stateParam = nvs.LastOrDefault(d => d.Key == "state").Value; if (state != null) { // We need some variables from our state parameter. NameValueCollection provideritem = HttpUtility.ParseQueryString(stateParam); if (provideritem["__provider__"] != null) { ProviderName = provideritem["__provider__"]; } if (provideritem["username"] != null) { username = provideritem["username"]; // Check to make sure username exists in database. if (!_accountRepository.Exists(d => d.UserName == username)) { return(Unauthorized()); } } // No username was provided. else { return(Unauthorized()); } if (provideritem["returnURI"] != null) { returnURI = provideritem["returnURI"]; } } else { return(BadRequest()); } } else { var nvs = Request.GetQueryNameValuePairs(); string stateParam = nvs.LastOrDefault(d => d.Key == "state").Value; if (state != null) { // We need some variables from our state parameter. NameValueCollection provideritem = HttpUtility.ParseQueryString(stateParam); if (provideritem["username"] != null) { username = provideritem["username"]; // Check to make sure username exists in database. if (!_accountRepository.Exists(d => d.UserName == username)) { return(Unauthorized()); } } // No username was provided. else { return(Unauthorized()); } if (provideritem["returnURI"] != null) { returnURI = provideritem["returnURI"]; } } else { return(BadRequest()); } } // Rewrite the request to include the requested headers and info for exchanging // the authorization code for a LinkedIn access token LinkedInOAuth2Client.RewriteRequest(); // Now that the request has been rewritten, make the call and include the same callback uri provided earlier var authResult = OpenAuth.VerifyAuthentication(_externalCallBack); // For future user when we integrate with other oauth2 applications string providerDisplayName = OpenAuth.GetProviderDisplayName(ProviderName); //If the verification process failed if (!authResult.IsSuccessful) { return(Unauthorized()); } else { //Get provider user details string providerUserId = authResult.ProviderUserId; string providerUserName = authResult.UserName; string firstName = null; string lastName = null; string accessToken = null; string email = null; if (email == null && authResult.ExtraData.ContainsKey("email-address")) { email = authResult.ExtraData["email-address"]; } if (firstName == null && authResult.ExtraData.ContainsKey("first-name")) { firstName = authResult.ExtraData["first-name"]; } if (lastName == null && authResult.ExtraData.ContainsKey("last-name")) { lastName = authResult.ExtraData["last-name"]; } if (accessToken == null && authResult.ExtraData.ContainsKey("accesstoken")) { accessToken = authResult.ExtraData["accesstoken"]; } var userInfo = new List <object>(); userInfo.Add(new { ProviderDisplayName = providerDisplayName, ProviderUserId = providerUserId, FirstName = firstName, LastName = lastName, Email = email, AccessToken = accessToken }); try { // If the given user already has a LinkedIn access token if (_linkedInAccessTokenRepository.Exists(d => d.UserName == username, d => d.Account)) { LinkedInAccessToken token = _linkedInAccessTokenRepository.GetSingle(d => d.UserName == username, d => d.Account); token.Expired = false; token.TokenCreation = DateTime.UtcNow; token.Value = accessToken; _linkedInAccessTokenRepository.Update(token); } else { LinkedInAccessToken token = new LinkedInAccessToken() { UserName = username, TokenCreation = DateTime.UtcNow, Value = accessToken }; _linkedInAccessTokenRepository.Insert(token); } } catch (Exception) { return(InternalServerError()); } if (returnURI != "null") { // Try the given redirectUri try { return(Redirect(returnURI + "?linkedin=success")); } // If it fails, go with the default catch (Exception) { return(Redirect("http://localhost:8080/Home?linkedin=success")); } } return(Redirect("http://localhost:8080/Home?linkedin=success")); } }