コード例 #1
0
        /// <summary>
        /// Construct a OAuth2 forwarding URI to redirect with.
        /// </summary>
        /// <param name="provider">OAuth2 provider wrapper.</param>
        /// <param name="redirectUri">URI to redirect back to the system.</param>
        /// <param name="locale">Language locale for provider interface.</param>
        /// <returns>URI to redirect system to, for user authorization.</returns>
        public static string CreateRedirect(OAuth2Provider provider, string redirectUri, string locale = "en")
        {
            var parameters = new Dictionary <string, string> {
                { "client_id", provider.ClientId },
                { "display", "page" },
                { "locale", locale },
                { "redirect_uri", redirectUri },
                { "response_type", "code" }
            };

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

            if (!string.IsNullOrWhiteSpace(provider.Scope))
            {
                parameters.Add("scope", provider.Scope);
            }

            if (!string.IsNullOrWhiteSpace(provider.State))
            {
                parameters.Add("state", provider.State);
            }

            var qs  = BuildQueryString(parameters);
            var url = provider.AuthUri + "?" + qs;

            return(url);
        }
コード例 #2
0
        /// <summary>
        /// Request a new access token by refreshing an old.
        /// </summary>
        /// <param name="provider">OAuth2 provider wrapper.</param>
        /// <param name="refreshToken">Access/refresh token to use.</param>
        /// <returns>Authentication response object.</returns>
        public static OAuth2AuthenticateResponse AuthenticateByToken(OAuth2Provider provider, string refreshToken)
        {
            var parameters = new Dictionary <string, string> {
                { "client_id", provider.ClientId },
                { "client_secret", provider.ClientSecret },
                { "refresh_token", refreshToken },
                { "grant_type", "refresh_token" }
            };

            if (!string.IsNullOrWhiteSpace(provider.Scope))
            {
                parameters.Add("scope", provider.Scope);
            }

            if (!string.IsNullOrWhiteSpace(provider.State))
            {
                parameters.Add("state", provider.State);
            }

            var reply = Request(
                provider.AccessTokenUri,
                payload: BuildQueryString(parameters));

            return(InterpretReply(reply));
        }
コード例 #3
0
        /// <summary>
        /// Get user info from the providers user endpoint.
        /// </summary>
        /// <param name="provider">OAuth2 provider wrapper.</param>
        /// <param name="accessToken">Access token to use.</param>
        /// <returns>Raw data from the provider.</returns>
        public static string GetUserInfo(OAuth2Provider provider, string accessToken)
        {
            var parameters = new Dictionary <string, string> {
                { "access_token", accessToken }
            };

            return(Request(provider.UserInfoUri, "GET", BuildQueryString(parameters)));
        }