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);
        }
Exemple #2
0
        private async void BtnConnect_Click(object sender, EventArgs e)
        {
            // create the oAuth-Client
            var lFactory = new OAuthClientFactory();

            oAuthClient = lFactory.Create(eConsumerKey.Text, eConsumerSecret.Text);

            // get the requesttoken
            Token requestToken;

            try
            {
                requestToken = await oAuthClient.GetRequestTokenAsync(eProtocolname.Text + "://" + callbackUrl);
            }
            catch (OAuthException ex)
            {
                MessageBox.Show(ex.Message);

                return;
            }

            eRequestKey.Text    = requestToken.Key;
            eRequestSecret.Text = requestToken.Secret;

            // get the url for authorization
            var lAuthorizeUrl = oAuthClient.GetAuthorizeUrl(requestToken);

            // starting authorization
            System.Diagnostics.Process.Start(lAuthorizeUrl.AbsoluteUri);

            // start host for listen
            host.Open();
        }
Exemple #3
0
        public void OAuth_GetAuthorizeUrl_Arg_empty_1()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = new Token(string.Empty, string.Empty);

            Uri url = oAuthClient.GetAuthorizeUrl(requestToken);
        }
Exemple #4
0
        public void OAuth_ClientFactory_Create()
        {
            OAuthClient oAuthClient;

            oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Assert.IsNotNull(oAuthClient);
        }
Exemple #5
0
        public void OAuth_GetAuthorizeUrl_Arg_empty_2()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = new Token(string.Empty, "erdertf");

            Uri url = oAuthClient.GetAuthorizeUrl(new Token(string.Empty, "ertdgfrt"));
        }
Exemple #6
0
        public async Task OAuth_GetAccessTokenAsync_Empty_1()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = await oAuthClient.GetRequestTokenAsync(_callbackUrl);

            Token accessToken = await oAuthClient.GetAccessTokenAsync(requestToken, string.Empty);
        }
Exemple #7
0
        public void OAuth_GetAuthorizeUrl_Arg_null_1()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Uri url;

            url = oAuthClient.GetAuthorizeUrl(null);
        }
Exemple #8
0
        public JsonResult GetOauthLoginUrl(OAuthSystemType systemtype)
        {
            var        client = OAuthClientFactory.GetOAuthClient(systemtype);
            JsonResult jr     = new JsonResult();

            jr.Data = new { url = client.GetAuthorizationUrl() };
            return(jr);
        }
Exemple #9
0
        public async Task OAuth_GetAuthorizeUrl_Arg_1()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = await oAuthClient.GetRequestTokenAsync(_callbackUrl);

            Uri url = oAuthClient.GetAuthorizeUrl(requestToken);

            Assert.IsNotNull(url);
        }
Exemple #10
0
        public async Task OAuth_GetAuthorizeUrl_Arg_2()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = await oAuthClient.GetRequestTokenAsync(_callbackUrl);

            Uri url = oAuthClient.GetAuthorizeUrl(requestToken);

            Assert.AreNotEqual(url, string.Empty);
        }
Exemple #11
0
        static void Main(string[] args)
        {
            var oAuthClient = OAuthClientFactory.GetOAuthClient("", "", "", AuthType.Sina);
            var url         = oAuthClient.GetAuthUrl();                             //获取验证地址

            Process.Start(@"C:\Program Files\Internet Explorer\IEXPLORE.EXE", url); //打开IE
            Console.Write("code:");
            var code        = Console.ReadLine();                                   //输入回调地址Url中code的值
            var accessToken = oAuthClient.GetAccessToken(code).Result;              //获取票据
            var user        = oAuthClient.GetUserInfo(accessToken).Result;          //获取用户信息

            Console.WriteLine(user.Name);
            Console.ReadKey();
        }
Exemple #12
0
        public ActionResult QQAuthorized(string code)
        {
            if (Request.Cookies["uid"] != null)
            {
                var c = new HttpCookie("uid");
                c.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(c);
            }
            bool result = OAuthClientFactory.AuthorizedCode(OAuthSystemType.QQ, code);

            if (result)
            {
                var oauthUserinfo = OAuthClientFactory.GetOAuthUserInfo(OAuthSystemType.QQ);
                UserHelper.HandleOauthUserLogIn(oauthUserinfo);
            }
            return(RedirectToAction("index", "home"));
        }
        private IOAuthClient GetOAuthClient(AuthType authType)
        {
            string clientId     = string.Empty;
            string clientSecret = string.Empty;
            string callbackUrl  = string.Empty;

            if (authType == AuthType.QQ)
            {
                clientId     = "";
                clientSecret = "";
                callbackUrl  = "https://" + Request.Host.Value + "CallbackUrl";
            }
            else if (authType == AuthType.Sina)
            {
                clientId     = "";
                clientSecret = "";
                callbackUrl  = "https://" + Request.Host.Value + "CallbackUrl";
            }
            return(OAuthClientFactory.GetOAuthClient(clientId, clientSecret, callbackUrl, authType));
        }
Exemple #14
0
        private async void Button1_Click(object sender, EventArgs e)
        {
            // creating oAuth-client
            OAuthClient oAuthClient = new OAuthClientFactory().Create(ConsumerKey.Text, ConsumerSecret.Text);

            // get requesttoken
            Token requestToken = await oAuthClient.GetRequestTokenAsync(_callbackUrl);

            // get the authorize Url
            Uri url = oAuthClient.GetAuthorizeUrl(requestToken);

            var verifierUrl = WebAuth.ShowDialog(url, _callbackUrl);

            Token accessToken = await oAuthClient.GetAccessTokenAsync(requestToken, verifierUrl.OriginalString);

            AccessKey.Text    = accessToken.Key;
            AccessSecret.Text = accessToken.Secret;

            Activate();

            UserInfo userInfo = null;

            try
            {
                var tc = new TumblrClientFactory().Create <TumblrClient>(ConsumerKey.Text, ConsumerSecret.Text, accessToken);
                userInfo = await tc.GetUserInfoAsync();
            }
            catch (Exception)
            {
                MessageBox.Show("Logon failure", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }


            MessageBox.Show($"Success! the name of your blog is {userInfo.Blogs[0].Name}", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        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);
        }
Exemple #16
0
        public void OAuth_ClientFactory_Create_Empty_3()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(string.Empty, _consumerSecret);
        }
Exemple #17
0
        public void OAuth_ClientFactory_Create_Empty_2()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(_consumerKey, string.Empty);
        }
Exemple #18
0
        public void OAuth_ClientFactory_Create_Empty_1()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(string.Empty, string.Empty);
        }
Exemple #19
0
        public void OAuth_ClientFactory_Create_Null_3()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(null, _consumerSecret);
        }
Exemple #20
0
        public void OAuth_GetAuthorizeUrl_Arg_null_3()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Uri url = oAuthClient.GetAuthorizeUrl(new Token(null, "ertdgfrt"));
        }
Exemple #21
0
        public void OAuth_ClientFactory_Create_Null_1()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(null, null);
        }
Exemple #22
0
        public async Task OAuth_GetAccessTokenAsync_Null_1()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token accessToken = await oAuthClient.GetAccessTokenAsync(null, null);
        }
Exemple #23
0
        public async Task OAuth_GetRequestTokenAsync_Empty()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create(_consumerKey, _consumerSecret);

            Token requestToken = await oAuthClient.GetRequestTokenAsync(string.Empty);
        }
 public IOAuthClient GetAPIOAuthClient()
 {
     return(OAuthClientFactory.GetOAuthClient(_configuration.QQAPIID,
                                              _configuration.QQAPIKey, _configuration.QQAPIAuthReturnURL, AuthType.QQ));
 }
Exemple #25
0
        public void OAuth_ClientFactory_Create_Null_2()
        {
            OAuthClient lOAuthClient;

            lOAuthClient = new OAuthClientFactory().Create(_consumerKey, null);
        }
 private IOAuthClient GetOAuthClient()
 {
     return(OAuthClientFactory.GetOAuthClient(configuration.QQAPPID,
                                              configuration.QQAPPKey, configuration.QQAuthReturnURL, AuthType.QQ));
 }
Exemple #27
0
        public async Task OAuth_GetRequestTokenAsync_Unauthorized()
        {
            OAuthClient oAuthClient = new OAuthClientFactory().Create("ertd", "ertg");

            Token requestToken = await oAuthClient.GetRequestTokenAsync(_callbackUrl);
        }