Exemplo n.º 1
0
        public ActionResult Authenticate(AuthRequestType authType)
        {
            Guard.MakeSureAllInstancesIsNullNot(_mySettings);

            var redirectUrl = string.Empty;

            switch (authType)
            {
                case AuthRequestType.Facebook:
                    var facebookContext = new FacebookAuthRequestProcessingContext();
                    var facebookInputParam = new FacebookAuthRequestProcessingParameter
                                                 {
                                                     RequestUrl = HttpContext.Request.GetUrlBase(),
                                                     RedirectPath = Url.Action("FacebookCallbackAction")
                                                 };

                    facebookContext.Execute(() => facebookInputParam);

                    redirectUrl = facebookContext.ReturnResult;
                    break;

                case AuthRequestType.Twitter:

                    var twitterContext = new TwitterAuthRequestProcessingContext();

                    var twitterCallbackUrl = Request.Url.ToString().Replace("Authenticate", "TwitterCallbackAction");
                    var twitterCallback = new Uri(twitterCallbackUrl);

                    _consumerKey = _mySettings.TwitterConsumerKey;
                    _secretKey = _mySettings.TwitterConsumerSecret;

                    AssertForKeyAndSecretConsummer(_consumerKey, _secretKey);

                    _tokenManager = new InMemoryTokenManager(_consumerKey, _secretKey);

                    var twitterInputParam = new TwitterAuthRequestProcessingParameter
                                                {
                                                    CallBackLink = twitterCallback,
                                                    TokenManager = _tokenManager
                                                };

                    twitterContext.Execute(() => twitterInputParam);

                    HttpContext.Application["TwitterTokenManager"] = _tokenManager;

                    return twitterContext.ReturnResult;

                    break;

                case AuthRequestType.Google:
                    var googleContext = new GoogleAuthRequestProcessingContext();

                    var callbackUrl = Request.Url.ToString().Replace("Authenticate", "GoogleCallbackAction");
                    var callback = new Uri(callbackUrl);

                    _consumerKey = _mySettings.GoogleConsumerKey;
                    _secretKey = _mySettings.GoogleConsumerSecret;

                    AssertForKeyAndSecretConsummer(_consumerKey, _secretKey);

                    _tokenManager = new InMemoryTokenManager(_consumerKey, _secretKey);

                    var googleInputParam = new GoogleAuthRequestProcessingParameter
                                               {
                                                   CallBackLink = callback,
                                                   TokenManager = _tokenManager
                                               };

                    googleContext.Execute(() => googleInputParam);

                    HttpContext.Application["GoogleTokenManager"] = _tokenManager;

                    return googleContext.ReturnResult;

                    break;
            }

            return Redirect(redirectUrl);
        }
        public ActionResult Authenticate(AuthRequestType authType)
        {
            Guard.MakeSureAllInstancesIsNullNot(_mySettings);

            var redirectUrl = string.Empty;

            switch (authType)
            {
            case AuthRequestType.Facebook:
                var facebookContext    = new FacebookAuthRequestProcessingContext();
                var facebookInputParam = new FacebookAuthRequestProcessingParameter
                {
                    RequestUrl   = HttpContext.Request.GetUrlBase(),
                    RedirectPath = Url.Action("FacebookCallbackAction")
                };

                facebookContext.Execute(() => facebookInputParam);

                redirectUrl = facebookContext.ReturnResult;
                break;

            case AuthRequestType.Twitter:

                var twitterContext = new TwitterAuthRequestProcessingContext();

                var twitterCallbackUrl = Request.Url.ToString().Replace("Authenticate", "TwitterCallbackAction");
                var twitterCallback    = new Uri(twitterCallbackUrl);

                _consumerKey = _mySettings.TwitterConsumerKey;
                _secretKey   = _mySettings.TwitterConsumerSecret;

                AssertForKeyAndSecretConsummer(_consumerKey, _secretKey);

                _tokenManager = new InMemoryTokenManager(_consumerKey, _secretKey);

                var twitterInputParam = new TwitterAuthRequestProcessingParameter
                {
                    CallBackLink = twitterCallback,
                    TokenManager = _tokenManager
                };

                twitterContext.Execute(() => twitterInputParam);

                HttpContext.Application["TwitterTokenManager"] = _tokenManager;

                return(twitterContext.ReturnResult);

                break;

            case AuthRequestType.Google:
                var googleContext = new GoogleAuthRequestProcessingContext();

                var callbackUrl = Request.Url.ToString().Replace("Authenticate", "GoogleCallbackAction");
                var callback    = new Uri(callbackUrl);

                _consumerKey = _mySettings.GoogleConsumerKey;
                _secretKey   = _mySettings.GoogleConsumerSecret;

                AssertForKeyAndSecretConsummer(_consumerKey, _secretKey);

                _tokenManager = new InMemoryTokenManager(_consumerKey, _secretKey);

                var googleInputParam = new GoogleAuthRequestProcessingParameter
                {
                    CallBackLink = callback,
                    TokenManager = _tokenManager
                };

                googleContext.Execute(() => googleInputParam);

                HttpContext.Application["GoogleTokenManager"] = _tokenManager;

                return(googleContext.ReturnResult);

                break;
            }

            return(Redirect(redirectUrl));
        }