Exemple #1
0
        //public ResponseModel<List<ConfigModel> > GetConfigSettings()
        //{
        //    return FetchFromCacheOrApi<List<ConfigModel>>(CacheKeys.SITE_CONFIG, ApiUrls.GetConfigSettings);
        //}
        public ResponseModel <ConfigModel> GetConfig()
        {
            var responseModel = new ResponseModel <ConfigModel>();
            var configModel   = CacheManager.Get <ConfigModel>(CacheKeys.SITE_CONFIG);

            if (configModel != null)
            {
                responseModel.Result = configModel;
                return(responseModel);
            }

            var response = CallApi <ConfigModel>(ApiUrls.GetConfigSettings, "");

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                configModel          = response.Result;
                responseModel.Result = configModel;

                //populate configData by extracting configSettings and populating all teh respective objects.
                ConvertConfigSettingsToClasses(ref configModel);

                //this is doen because at times, an error is thrown and the error is cached fro the respective key.
                // so, if an error is found in the message, the data is NOT cached by the key
                CacheManager.Set(CacheKeys.SITE_CONFIG, configModel);
            }

            //register the social clients after fetching the details from the API.
            /// this registration is NOT called when the data is fetched from CACHE.
            OAuthClientFactory.RegisterTwitterClient(configModel.SocialSettings.TwitterApiKey, configModel.SocialSettings.TwitterApiSecret, configModel.SocialSettings.TwitterUrl);
            OAuthClientFactory.RegisterGoogleClient(configModel.SocialSettings.GooglePlusApiKey, configModel.SocialSettings.GooglePlusApiSecret, configModel.SocialSettings.GooglePlusUrl);
            OAuthClientFactory.RegisterFacebookClient(configModel.SocialSettings.FacebookApiKey, configModel.SocialSettings.FacebookApiSecret, configModel.SocialSettings.FacebookUrl);

            return(responseModel);
            //return FetchFromCacheOrApi<ConfigModel>(CacheKeys.SITE_CONFIG, ApiUrls.GetConfigSettings);
        }
        public ActionResult SocialSignIn(string provider = "None")
        {
            string  recordId = "";
            dynamic user     = null;

            //Addtional check in case client not registered properly.
            //only happens when project throws an exception or restarted.
            if (!OAuthClientFactory.IsFacebookRegistered)
            {
                OAuthClientFactory.RegisterFacebookClient(_sessionContext.CurrentSiteConfig.SocialSettings.FacebookApiKey, _sessionContext.CurrentSiteConfig.SocialSettings.FacebookApiSecret, _sessionContext.CurrentSiteConfig.SocialSettings.FacebookUrl);
            }
            if (!OAuthClientFactory.IsGoogleRegistered)
            {
                OAuthClientFactory.RegisterGoogleClient(_sessionContext.CurrentSiteConfig.SocialSettings.GooglePlusApiKey, _sessionContext.CurrentSiteConfig.SocialSettings.GooglePlusApiSecret, _sessionContext.CurrentSiteConfig.SocialSettings.GooglePlusUrl);
            }
            if (!OAuthClientFactory.IsTwitterRegisterd)
            {
                OAuthClientFactory.RegisterTwitterClient(_sessionContext.CurrentSiteConfig.SocialSettings.TwitterApiKey, _sessionContext.CurrentSiteConfig.SocialSettings.TwitterApiSecret, _sessionContext.CurrentSiteConfig.SocialSettings.TwitterUrl);
            }

            var response = _socialService.SocialSignIn(provider);

            if (response != null)
            {
                var obj = JObject.Parse(response.JsonResponse);
                switch (response.OAuthClient)
                {
                case OAuthClient.Twitter:
                {
                    user = new TwitterResponse()
                    {
                        Source    = Enum.GetName(typeof(OAuthClient), response.OAuthClient),
                        FullName  = Convert.ToString(obj["name"]),
                        UserName  = Convert.ToString(obj["screen_name"]),
                        SocialId  = Convert.ToString(obj["id"]),
                        Email     = Convert.ToString(obj["email"]),
                        Location  = Convert.ToString(obj["location"]),
                        FirstName = Convert.ToString(obj["name"]).Split(' ')[0],
                        LastName  = Convert.ToString(obj["name"]).Split(' ')[1],
                    };

                    var siteUser = _customerRepository.GetExistingUser(user.Email);
                    if (siteUser.Result.Count == 0)         //user not exits, new user
                    {
                        CustomerModel customer = new CustomerModel()
                        {
                            Email        = user.Email,
                            FirstName    = user.FirstName,
                            LastName     = user.LastName,
                            IsRegistered = false
                        };

                        var result = _customerRepository.Register(customer);
                        recordId      = result.Result.RecordId;
                        user.RecordId = recordId;

                        CloudTableRepository _cloudRepository = new CloudTableRepository();
                        var table = _cloudRepository.CreateTable("SocialUsers");
                        _cloudRepository.InsertUser(table, user); //insert entry in azure table
                    }
                    else                                          //existing user
                    {
                        recordId = Convert.ToString(siteUser.Result[0].UserId);
                    }
                }
                break;

                case OAuthClient.Facebook:
                {
                    user = new FacebookResponse()
                    {
                        Source      = Enum.GetName(typeof(OAuthClient), response.OAuthClient),
                        FullName    = Convert.ToString(obj["name"]),
                        FirstName   = Convert.ToString(obj["first_name"]),
                        LastName    = Convert.ToString(obj["last_name"]),
                        SocialId    = Convert.ToString(obj["id"]),
                        Email       = Convert.ToString(obj["email"]),
                        Gender      = Convert.ToString(obj["gender"]),
                        Location    = Convert.ToString(obj["location"]),
                        HomeTown    = Convert.ToString(obj["hometown"]),
                        DateOfBirth = Convert.ToString(obj["birthday"])
                    };

                    var siteUser = _customerRepository.GetExistingUser(user.Email);
                    if (siteUser.Result.Count == 0)         //user not exits, new user
                    {
                        CustomerModel customer = new CustomerModel()
                        {
                            Email        = user.Email,
                            FirstName    = user.FirstName,
                            LastName     = user.LastName,
                            Gender       = user.Gender,
                            IsRegistered = false
                        };

                        var result = _customerRepository.Register(customer);
                        recordId      = result.Result.RecordId;
                        user.RecordId = recordId;

                        CloudTableRepository _cloudRepository = new CloudTableRepository();
                        var table = _cloudRepository.CreateTable("SocialUsers");
                        _cloudRepository.InsertUser(table, user);
                    }
                    else         //existing user
                    {
                        recordId = Convert.ToString(siteUser.Result[0].UserId);
                    }
                }
                break;

                case OAuthClient.Google:
                {
                    user = new GoogleResponse()
                    {
                        Source    = Enum.GetName(typeof(OAuthClient), response.OAuthClient),
                        SocialId  = Convert.ToString(obj["id"]),
                        FullName  = Convert.ToString(obj["name"]),
                        FirstName = Convert.ToString(obj["given_name"]),
                        LastName  = Convert.ToString(obj["family_name"]),
                        Email     = Convert.ToString(obj["email"]),
                    };

                    var siteUser = _customerRepository.GetExistingUser(user.Email);
                    if (siteUser.Result.Count == 0)         //user not exits, new user
                    {
                        CustomerModel customer = new CustomerModel()
                        {
                            Email        = user.Email,
                            FirstName    = user.FirstName,
                            LastName     = user.LastName,
                            IsRegistered = false
                        };

                        var result = _customerRepository.Register(customer);
                        recordId      = result.Result.RecordId;
                        user.RecordId = recordId;

                        CloudTableRepository _cloudRepository = new CloudTableRepository();
                        var table = _cloudRepository.CreateTable("SocialUsers");
                        _cloudRepository.InsertUser(table, user);
                    }
                    else         //existing user
                    {
                        recordId = Convert.ToString(siteUser.Result[0].UserId);
                    }
                }
                break;

                default:
                    break;
                }

                var results = _authenticationService.SocialLogin(recordId);
                if (results == null)
                {
                    return(JsonValidationError());
                }
                else
                {
                    return(Redirect("/"));
                }
            }
            return(null);
        }