Beispiel #1
0
        public static SocialUserModel BindUserModel()
        {
            SocialUserModel result = null;

            if (IsLinkedInCodeRedirect())
            {
                result = GetSocialProfileFormSession().UserModel;
            }
            else if (ShouldUseSocialProfileFromSession)
            {
                result = GetSocialProfileFormSession().UserModel;
            }
            else if (HttpContext.Current != null)
            {
                var request = HttpContext.Current.Request;

                result = new SocialUserModel(Helper.DecodeUrlParameter(request["firstname"]), Helper.DecodeUrlParameter(request["middlename"]), Helper.DecodeUrlParameter(request["lastname"]), Helper.DecodeUrlParameter(request["email"]), Helper.DecodeUrlParameter(request["avatarurl"]));
            }

            return(result);
        }
Beispiel #2
0
 public SocialProfile(SocialAuthenticationModel authenticationModel, SocialUserModel userModel)
 {
     AuthenticationModel = authenticationModel;
     UserModel           = userModel;
 }
Beispiel #3
0
        private static SocialAuthenticationModel GetLinkedInAuthenticationModelAsync()
        {
            var result = GetSocialProfileFormSession() != null?GetSocialProfileFormSession().AuthenticationModel : null;

            if (result == null)
            {
                var url     = ConfigurationManager.AppSettings["LinkedInAccessTokenUrl"];
                var headers = new Dictionary <String, String> {
                    { "Content-Type", "application/x-www-form-urlencoded" }
                };
                var nvc = new NameValueCollection();

                nvc.Add("grant_type", "authorization_code");
                nvc.Add("code", HttpContext.Current.Request.QueryString["code"]);
                nvc.Add("redirect_uri", Helper.GetUrlEncodedString((clsUtility.GetRootHost + "login.aspx").ToUri().UrlToHttps().ToString().ToLower()));
                nvc.Add("client_id", ConfigurationManager.AppSettings["LinkedInAppId"]);
                nvc.Add("client_secret", ConfigurationManager.AppSettings["LinkedInAppSecret"]);

                var data     = nvc.NameValueToQueryString().Substring(1);
                var response = Helper.DeserializeJsonToObject(Helper.RequestUrlAsync(url, "POST", data, headers)) as Dictionary <String, Object>;

                if (response.ContainsKey("error") || response.ContainsKey("error_description"))
                {
                    throw new Exception(String.Format("LinkedIn authentication error: \"{0}\"", response["error_description"]));
                }

                var accessToken = response["access_token"] as String;

                url     = ConfigurationManager.AppSettings["LinkedInApiUrl"] + "people/~:(id,first-name,last-name,email-address,picture-url)";
                headers = new Dictionary <String, String> {
                    { "Authorization", "Bearer " + accessToken },
                    { "x-li-format", "json" }
                };
                response = Helper.DeserializeJsonToObject(Helper.RequestUrlAsync(url, "GET", null, headers)) as Dictionary <String, Object>;

                if (response.ContainsKey("error") || response.ContainsKey("error_description"))
                {
                    throw new Exception(String.Format("LinkedIn api error: \"{0}\"", response["error_description"]));
                }
                else if (response.ContainsKey("emailAddress"))
                {
                    if ((response["emailAddress"] as String).HasText())
                    {
                        result = new SocialAuthenticationModel(response["id"] as String, response["emailAddress"] as String, accessToken, SocialAuthenticationProvidersEnum.LinkedIn.ToString());
                        url    = ConfigurationManager.AppSettings["LinkedInApiUrl"] + "people/" + response["id"] + "/picture-urls::(original)";

                        var avatarUrl = response["pictureUrl"] as String;

                        try
                        {
                            var pictureUrlsResponse = Helper.DeserializeJsonToObject(Helper.RequestUrlAsync(url, "GET", null, headers)) as Dictionary <String, Object>;

                            if (pictureUrlsResponse.ContainsKey("values"))
                            {
                                var pus = pictureUrlsResponse["values"] as Object[];

                                if (pus != null && pus.Length > 0)
                                {
                                    avatarUrl = pus[0] as String;
                                }
                            }
                        }
                        catch
                        {
                        }

                        var userModel = new SocialUserModel(response["firstName"] as String, null, response["lastName"] as String, response["emailAddress"] as String, avatarUrl);

                        SetSocialProfileToSession(new SocialProfile(result, userModel));
                    }
                    else
                    {
                        throw new Exception("LinkedIn profile: email address is empty.");
                    }
                }
                else
                {
                    throw new Exception("LinkedIn api: profile does not contain email-address field which is required.");
                }
            }

            return(result);
        }