/// <summary>Creates the URI to use to request an authorization code.</summary>
        /// <param name="redirectUri">The URI that should receive the authorization code; use <see cref="OutOfBandUri"/> for out-of-band requests.</param>
        /// <param name="scope">The authorization scopes that should be included in the authorization code.</param>
        /// <param name="state">An optional string that will be included in the response sent to <paramref name="redirectUri"/>.</param>
        /// <param name="offlineAccess">Requests offline use (a refresh token will be provided alongside the access token).</param>
        /// <param name="forcePrompt">If true, the user will be required to confirm authorization even if the requested scopes have already been granted.</param>
        /// <returns>The generated URI.</returns>
        public Uri CreateAuthorizationRequest(Uri redirectUri, AuthorizationScope scope, string?state = null, bool offlineAccess = false, bool forcePrompt = false)
        {
            if (scope == AuthorizationScope.None)
            {
                throw new ArgumentException("At least one authorization scope must be selected.", nameof(scope));
            }
            var uri   = this.BuildEndPointUri(OAuth2.AuthorizationEndPoint);
            var query = new StringBuilder();

            query.Append("response_type=code");
            query.Append("&client_id=").Append(Uri.EscapeDataString(this.ClientId));
            query.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
            query.Append("&scope=").Append(string.Join("+", OAuth2.ScopeStrings(scope)));
            if (state != null)
            {
                query.Append("&state=").Append(Uri.EscapeDataString(state));
            }
            if (offlineAccess)
            {
                query.Append("&access_type=offline");
            }
            if (forcePrompt)
            {
                query.Append("&approval_prompt=force");
            }
            uri.Query = query.ToString();
            return(uri.Uri);
        }
Example #2
0
        /// <summary>Creates the URI to use to request an authorization code.</summary>
        /// <param name="redirectUri">The URI that should receive the authorization code; use <see cref="OutOfBandUri"/> for out-of-band requests.</param>
        /// <param name="scope">The authorization scopes that should be included in the authorization code.</param>
        /// <param name="state">An optional string that will be included in the response sent to <paramref name="redirectUri"/>.</param>
        /// <param name="offlineAccess">Requests offline use (a refresh token will be provided alongside the access token).</param>
        /// <param name="forcePrompt">If true, the user will be required to confirm authorization even if the requested scopes have already been granted.</param>
        /// <returns>The generated URI.</returns>
        public Uri CreateAuthorizationRequest(Uri redirectUri, AuthorizationScope scope, string state = null, bool offlineAccess = false, bool forcePrompt = false)
        {
            if (redirectUri == null)
            {
                throw new ArgumentNullException(nameof(redirectUri));
            }
            if (scope == AuthorizationScope.None)
            {
                throw new ArgumentException("At least one authorization scope must be selected.", nameof(scope));
            }
            if (this.WebSite == null || this.WebSite.Trim().Length == 0)
            {
                throw new InvalidOperationException("No website has been set.");
            }
            if (this.ClientId == null || this.ClientId.Trim().Length == 0)
            {
                throw new InvalidOperationException("No client ID has been set.");
            }
            var uri   = new UriBuilder(this.UrlScheme, this.WebSite, this.Port, OAuth2.AuthorizationEndPoint);
            var query = new StringBuilder();

            query.Append("response_type=code");
            query.Append("&client_id=").Append(Uri.EscapeDataString(this.ClientId));
            query.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
            query.Append("&scope=").Append(string.Join("+", OAuth2.ScopeStrings(scope)));
            if (state != null)
            {
                query.Append("&state=").Append(Uri.EscapeDataString(state));
            }
            if (offlineAccess)
            {
                query.Append("&access_type=offline");
            }
            if (forcePrompt)
            {
                query.Append("&approval_prompt=force");
            }
            uri.Query = query.ToString();
            return(uri.Uri);
        }