Пример #1
0
        public ActionResult Authenticate(string success, string failure, string permissions, string state, bool offline)
        {
            string key = Guid.NewGuid().ToStringValue();

            OAuthState authState = new OAuthState();

            authState.FailureUrl = failure;
            authState.SuccessUrl = success;
            authState.State      = state;

            this.stateManager.SaveState(key, authState);

            IDictionary <string, string> parameters = new Dictionary <string, string>();

            if (offline)
            {
                parameters.Add("access_type", "offline");
            }

            List <string> permissionList = new List <string>();

            if (!string.IsNullOrWhiteSpace(permissions))
            {
                permissionList.AddRange(permissions.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries));
            }

            GoogleClient client = new GoogleClient(this.context.Config.Social.Google.AppID,
                                                   this.context.Config.Social.Google.AppSecret);

            string authorizationUrl = client.BuildAuthorizationUrl(SocialApiSetting.BuildUrl(this.context.Config.Social.Google.Domain, "social/google/authorize"), permissionList, key, parameters);

            return(new RedirectResult(authorizationUrl));
        }
        public ActionResult Authenticate(string success, string failure, string state)
        {
            string key = Guid.NewGuid().ToStringValue();

            OAuthState authState = new OAuthState();

            authState.FailureUrl = failure;
            authState.SuccessUrl = success;
            authState.State      = state;

            this.stateManager.SaveState(key, authState);
            UrlBuilder urlBuilder = new UrlBuilder(SocialApiSetting.BuildUrl(this.context.Config.Social.Twitter.Domain, "social/twitter/authorize"));

            urlBuilder.QueryString.Add("state", key);

            TwitterClient client = new TwitterClient(this.context.Config.Social.Twitter.AppKey,
                                                     this.context.Config.Social.Twitter.AppSecret);

            var tempCredential = client.GetRequestToken(urlBuilder.ToString());

            if (tempCredential == null || !tempCredential.OAuthCallbackConfirmed)
            {
                return(new RedirectResult(failure));
            }

            var           authorizationUrl = client.BuildAuthorizationUrl(tempCredential);
            ITokenManager tokenManager     = Container.Get <ITokenManager>();

            tokenManager.SaveRequestToken(key, tempCredential);
            return(new RedirectResult(authorizationUrl));
        }
Пример #3
0
        public ActionResult Authorize(string state, string code, string error)
        {
            OAuthState authState = this.stateManager.GetState(state);

            if (authState == null)
            {
                throw new InvalidOperationException("Invalid Authorization State");
            }

            UrlBuilder errorUrlBuilder = new UrlBuilder(authState.FailureUrl);

            if (string.IsNullOrWhiteSpace(error))
            {
                GoogleClient client = new GoogleClient(this.context.Config.Social.Google.AppID, this.context.Config.Social.Google.AppSecret);

                var credential = client.GetAccessToken(code, SocialApiSetting.BuildUrl(this.context.Config.Social.Google.Domain, "social/google/authorize"));
                if (credential != null && credential.Success)
                {
                    UrlBuilder redirectBuilder = new UrlBuilder(authState.SuccessUrl);
                    redirectBuilder.QueryString.Add("token", credential.Token);
                    redirectBuilder.QueryString.Add("refreshToken", credential.RefreshToken);
                    redirectBuilder.QueryString.Add("expiresIn", credential.ExpiresIn.ToStringValue());

                    if (!string.IsNullOrWhiteSpace(authState.State))
                    {
                        redirectBuilder.QueryString.Add("state", authState.State);
                    }

                    return(new RedirectResult(redirectBuilder.ToString()));
                }

                if (credential != null && !string.IsNullOrWhiteSpace(credential.ErrorCode))
                {
                    errorUrlBuilder.QueryString.Add("code", credential.ErrorCode);
                    errorUrlBuilder.QueryString.Add("message", credential.ErrorMessage);
                }
            }
            errorUrlBuilder.QueryString.Add("code", error);


            return(new RedirectResult(errorUrlBuilder.ToString()));
        }