/// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>. If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, Uri redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false)
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("clientId");
            }

            if (redirectUri == null && oauthResponseType != OAuthResponseType.Code)
            {
                throw new ArgumentNullException("redirectUri");
            }

            var queryBuilder = new StringBuilder();

            queryBuilder.Append("response_type=");
            switch (oauthResponseType)
            {
            case OAuthResponseType.Token:
                queryBuilder.Append("token");
                break;

            case OAuthResponseType.Code:
                queryBuilder.Append("code");
                break;

            default:
                throw new ArgumentOutOfRangeException("oauthResponseType");
            }

            queryBuilder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));

            if (redirectUri != null)
            {
                queryBuilder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
            }

            if (!string.IsNullOrWhiteSpace(state))
            {
                queryBuilder.Append("&state=").Append(Uri.EscapeDataString(state));
            }

            if (forceReapprove)
            {
                queryBuilder.Append("&force_reapprove=true");
            }

            if (disableSignup)
            {
                queryBuilder.Append("&disable_signup=true");
            }

            var uriBuilder = new UriBuilder("https://www.dropbox.com/1/oauth2/authorize")
            {
                Query = queryBuilder.ToString()
            };

            return(uriBuilder.Uri);
        }
Пример #2
0
 public string OAuthDialogUri(FBPermissions Permissions, OAuthResponseType ResponseType, string ClientId, string RedirectUri)
 {
     return oauthDialogUri + "/?" +
         "scope=" + "" + "&" +
         "client_id=" + ClientId + "&" +
         "redirect_uri=" + RedirectUri + "&" +
         "response_type=" + Enum.GetName(typeof(OAuthResponseType), ResponseType).ToLower();
         ;
 }
Пример #3
0
 /// <summary>
 /// Creates a new <see cref="AuthorizationContext"/>.
 /// </summary>
 /// <param name="httpContext">The current <see cref="HttpContext"/>.</param>
 /// <param name="clientId">The client identifier of the application.</param>
 /// <param name="redirectUri">The redirect uri of the application.</param>
 /// <param name="responseType">The response type (AuthorizationCode or AccessToken)</param>
 /// <param name="scope">The OAuth scopes.</param>
 /// <param name="state">The state passed from client to prevent cross-site request forgery.</param>
 public AuthorizationContext(HttpContext httpContext, string clientId, Uri redirectUri, OAuthResponseType responseType, IEnumerable <string> scope, string state)
     : base(httpContext)
 {
     ClientId     = Guard.ArgumentNotNullOrWhiteSpace(clientId, nameof(clientId));
     RedirectUri  = Guard.ArgumentNotNull(redirectUri, nameof(redirectUri));
     ResponseType = responseType;
     Scopes       = scope ?? new string[0];
     State        = state;
 }
Пример #4
0
        /// <summary>
        /// 得到请求code的url
        /// </summary>
        /// <param name="oauthResponseType"></param>
        /// <param name="endPoint">请求终结点</param>
        /// <param name="clientId">clientId</param>
        /// <param name="redirectUri">重定向url</param>
        /// <param name="state"></param>
        /// <param name="forceReapprove"></param>
        /// <param name="disableSignup"></param>
        /// <param name="requireRole"></param>
        /// <returns></returns>
        public string GetAuthorizeUri(OAuthResponseType oauthResponseType, string endPoint, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null)
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("clientId");
            }
            if ((redirectUri == null) && (oauthResponseType != OAuthResponseType.Code))
            {
                throw new ArgumentNullException("redirectUri");
            }

            StringBuilder builder = new StringBuilder();

            builder.Append("response_type=");
            switch (oauthResponseType)
            {
            case OAuthResponseType.Token:
                builder.Append("token");
                break;

            case OAuthResponseType.Code:
                builder.Append("code");
                break;

            default:
                throw new ArgumentOutOfRangeException("oauthResponseType");
            }
            builder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));
            if (!string.IsNullOrEmpty(redirectUri))
            {
                builder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri));
            }
            if (!string.IsNullOrWhiteSpace(state))
            {
                builder.Append("&state=").Append(Uri.EscapeDataString(state));
            }
            if (forceReapprove)
            {
                builder.Append("&force_reapprove=true");
            }
            if (disableSignup)
            {
                builder.Append("&disable_signup=true");
            }
            if (!string.IsNullOrWhiteSpace(requireRole))
            {
                builder.Append("&require_role=").Append(requireRole);
            }
            UriBuilder builder2 = new UriBuilder(endPoint)
            {
                Query = builder.ToString()
            };

            return(builder2.Uri.ToString());
        }
Пример #5
0
        /// <summary>
        /// Create authoriztion endpoint specific request context.
        /// </summary>
        /// <param name="httpContext">The current HTTP request specific <see cref="HttpContext"/>.</param>
        /// <returns>The task to create the authoriztion endpoint specific request context.</returns>
        /// <exception cref="ArgumentNullException">Specified <paramref name="httpContext"/> is null.</exception>
        public Task <AuthorizationContext> CreateAuthorizationContextAsync(HttpContext httpContext)
        {
            Guard.ArgumentNotNull(httpContext, nameof(httpContext));
            var query = httpContext.Request.Query;

            //Extract redirect_uri
            var redirectUriString = query.GetValue(OAuthDefaults.ParameterNames.RedirectUri);
            Uri redirectUri;

            try
            {
                redirectUri = new Uri(redirectUriString);
            }
            catch
            {
                return(Task.FromResult(new AuthorizationContext(httpContext, OAuthErrors.InvalidRequest.InvalidRedirectUri.Format())));
            }

            //Extract response_type
            var responseTypeString = query.GetValue(OAuthDefaults.ParameterNames.ResponseType);

            if (string.IsNullOrWhiteSpace(responseTypeString))
            {
                return(Task.FromResult(new AuthorizationContext(httpContext, OAuthErrors.InvalidRequest.MissingResponseType.Format(), redirectUri)));
            }

            //Validate response_type
            if (!_validResponseTypes.Contains(responseTypeString))
            {
                return(Task.FromResult(new AuthorizationContext(httpContext, OAuthErrors.UnsupportedResponseType.UnsupportedResponseType.Format(responseTypeString), redirectUri)));
            }

            OAuthResponseType responseType = responseTypeString == "code"
                ? OAuthResponseType.AuthorizationCode
                : OAuthResponseType.AccessToken;

            //Extract client_id
            var clientId = query.GetValue(OAuthDefaults.ParameterNames.ClientId);

            if (string.IsNullOrWhiteSpace(clientId))
            {
                return(Task.FromResult(new AuthorizationContext(httpContext, OAuthErrors.InvalidRequest.MissingClientId.Format(), redirectUri)));
            }

            var state = query.GetValue(OAuthDefaults.ParameterNames.State);
            var scope = query.GetValue(OAuthDefaults.ParameterNames.Scope);

            if (!string.IsNullOrWhiteSpace(scope))
            {
                return(Task.FromResult(new AuthorizationContext(httpContext, clientId, redirectUri, responseType, scope.Split(' '), state)));
            }
            return(Task.FromResult(new AuthorizationContext(httpContext, clientId, redirectUri, responseType, _defaultScope, state)));
        }
Пример #6
0
        public static Uri GetAuthorizeUri(
            OAuthResponseType oauthResponseType, 
            string authUri, 
            string clientId, 
            string redirectUri = null)
        {
            bool forceReapprove = false;
            bool disableSignup = false;

            if (string.IsNullOrWhiteSpace(clientId)) throw new ArgumentNullException("clientId");
            if (redirectUri == null && oauthResponseType != OAuthResponseType.Code) throw new ArgumentNullException("redirectUri");

            var queryBuilder = new StringBuilder();

            queryBuilder.Append("response_type=");
            switch (oauthResponseType)
            {
                case OAuthResponseType.Token:
                    queryBuilder.Append("token");
                    break;
                case OAuthResponseType.Code:
                    queryBuilder.Append("code");
                    break;
                default:
                    throw new ArgumentOutOfRangeException("oauthResponseType");
            }

            queryBuilder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));

            if (redirectUri != null)
            {
                queryBuilder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri));
            }

            if (forceReapprove)
            {
                queryBuilder.Append("&force_reapprove=true");
            }

            if (disableSignup)
            {
                queryBuilder.Append("&disable_signup=true");
            }

            var uriBuilder = new UriBuilder(authUri)
            {
                Query = queryBuilder.ToString()
            };

            return uriBuilder.Uri;
        }
        /// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <param name="requireRole"If this parameter is specified, the user will be asked to authorize with a particular
        /// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
        /// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
        /// parameter.</param>
        /// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
        /// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
        /// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
        /// different account.</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false)
        {
            var uri = string.IsNullOrEmpty(redirectUri) ? null : new Uri(redirectUri);

            return(GetAuthorizeUri(oauthResponseType, clientId, uri, state, forceReapprove, disableSignup, requireRole, forceReauthentication));
        }
        /// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code. 
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>. If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false)
        {
            var uri = string.IsNullOrEmpty(redirectUri) ? null : new Uri(redirectUri);

            return GetAuthorizeUri(oauthResponseType, clientId, uri, state, forceReapprove, disableSignup);
        }
Пример #9
0
        /// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
        /// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
        /// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
        /// parameter.</param>
        /// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
        /// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
        /// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
        /// different account.</param>
        /// <param name="tokenAccessType">Determines the type of token to request.  See <see cref="TokenAccessType" />
        /// for information on specific types available.  If none is specified, this will use the legacy type.</param>
        /// <param name="scopeList">list of scopes to request in base oauth flow.  If left blank, will default to all scopes for app</param>
        /// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, Uri redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None
                                          )
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("clientId");
            }

            if (redirectUri == null && oauthResponseType != OAuthResponseType.Code)
            {
                throw new ArgumentNullException("redirectUri");
            }

            var queryBuilder = new StringBuilder();

            queryBuilder.Append("response_type=");
            switch (oauthResponseType)
            {
            case OAuthResponseType.Token:
                queryBuilder.Append("token");
                break;

            case OAuthResponseType.Code:
                queryBuilder.Append("code");
                break;

            default:
                throw new ArgumentOutOfRangeException("oauthResponseType");
            }

            queryBuilder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));

            if (redirectUri != null)
            {
                queryBuilder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
            }

            if (!string.IsNullOrWhiteSpace(state))
            {
                queryBuilder.Append("&state=").Append(Uri.EscapeDataString(state));
            }

            if (forceReapprove)
            {
                queryBuilder.Append("&force_reapprove=true");
            }

            if (disableSignup)
            {
                queryBuilder.Append("&disable_signup=true");
            }

            if (!string.IsNullOrWhiteSpace(requireRole))
            {
                queryBuilder.Append("&require_role=").Append(requireRole);
            }

            if (forceReauthentication)
            {
                queryBuilder.Append("&force_reauthentication=true");
            }

            if (tokenAccessType != TokenAccessType.Legacy)
            {
                queryBuilder.Append("&token_access_type=").Append(tokenAccessType.ToString().ToLower());
            }

            if (scopeList != null)
            {
                queryBuilder.Append("&scope=").Append(String.Join(" ", scopeList));
            }

            if (includeGrantedScopes != IncludeGrantedScopes.None)
            {
                queryBuilder.Append("&include_granted_scopes=").Append(includeGrantedScopes.ToString().ToLower());
            }

            var uriBuilder = new UriBuilder("https://www.dropbox.com/oauth2/authorize")
            {
                Query = queryBuilder.ToString()
            };

            return(uriBuilder.Uri);
        }
Пример #10
0
        /// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
        /// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
        /// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
        /// parameter.</param>
        /// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
        /// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
        /// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
        /// different account.</param>
        /// <param name="tokenAccessType">Determines the type of token to request.  See <see cref="TokenAccessType" />
        /// for information on specific types available.  If none is specified, this will use the legacy type.</param>
        /// <param name="scopeList">list of scopes to request in base oauth flow.  If left blank, will default to all scopes for app</param>
        /// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None)
        {
            var uri = string.IsNullOrEmpty(redirectUri) ? null : new Uri(redirectUri);

            return(GetAuthorizeUri(oauthResponseType, clientId, uri, state, forceReapprove, disableSignup, requireRole, forceReauthentication, tokenAccessType, scopeList, includeGrantedScopes));
        }
 /// <summary>
 /// Gets the URI used to start the OAuth2.0 authorization flow.  Passes in codeChallenge generated in this class
 /// </summary>
 /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
 /// <param name="clientId">The apps key, found in the
 /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
 /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
 /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
 /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code.
 /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
 /// the information in your app.</param>
 /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
 /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
 /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
 /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
 /// <paramref name="redirectUri"/>If <c>true</c>, the user will not be automatically redirected and will have to approve
 /// the app again.</param>
 /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
 /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
 /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
 /// <param name="requireRole">If this parameter is specified, the user will be asked to authorize with a particular
 /// type of Dropbox account, either work for a team account or personal for a personal account. Your app should still
 /// verify the type of Dropbox account after authorization since the user could modify or remove the require_role
 /// parameter.</param>
 /// <param name="forceReauthentication"> If <c>true</c>, users will be signed out if they are currently signed in.
 /// This will make sure the user is brought to a page where they can create a new account or sign in to another account.
 /// This should only be used when there is a definite reason to believe that the user needs to sign in to a new or
 /// different account.</param>
 /// <param name="tokenAccessType">Determines the type of token to request.  See <see cref="TokenAccessType" />
 /// for information on specific types available.  If none is specified, this will use the legacy type.</param>
 /// <param name="scopeList">list of scopes to request in base oauth flow.  If left blank, will default to all scopes for app</param>
 /// <param name="includeGrantedScopes">which scopes to include from previous grants. Note: if this user has never linked the app, include_granted_scopes must be None</param>
 /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
 public Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, string redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false, string requireRole = null, bool forceReauthentication = false, TokenAccessType tokenAccessType = TokenAccessType.Legacy, string[] scopeList = null, IncludeGrantedScopes includeGrantedScopes = IncludeGrantedScopes.None)
 {
     return(DropboxOAuth2Helper.GetAuthorizeUri(oauthResponseType, clientId, redirectUri, state, forceReapprove, disableSignup, requireRole, forceReauthentication, tokenAccessType, scopeList, includeGrantedScopes, this.CodeChallenge));
 }
Пример #12
0
 public string GetAuthorizationUrl(OAuthResponseType oAuth2ResponseType, string state = null) => $"{AuthorizationEndPointUrl}?client_id={ApiClient.ClientId}&response_type={oAuth2ResponseType.ToString().ToLower()}&state={state}";
        /// <summary>
        /// Gets the URI used to start the OAuth2.0 authorization flow.
        /// </summary>
        /// <param name="oauthResponseType">The grant type requested, either <c>Token</c> or <c>Code</c>.</param>
        /// <param name="clientId">The apps key, found in the
        /// <a href="https://www.dropbox.com/developers/apps">App Console</a>.</param>
        /// <param name="redirectUri">Where to redirect the user after authorization has completed. This must be the exact URI
        /// registered in the <a href="https://www.dropbox.com/developers/apps">App Console</a>; even <c>localhost</c>
        /// must be listed if it is used for testing. A redirect URI is required for a token flow, but optional for code. 
        /// If the redirect URI is omitted, the code will be presented directly to the user and they will be invited to enter
        /// the information in your app.</param>
        /// <param name="state">Up to 500 bytes of arbitrary data that will be passed back to <paramref name="redirectUri"/>.
        /// This parameter should be used to protect against cross-site request forgery (CSRF).</param>
        /// <param name="forceReapprove">Whether or not to force the user to approve the app again if they've already done so.
        /// If <c>false</c> (default), a user who has already approved the application may be automatically redirected to
        /// <paramref name="redirectUri"/>. If <c>true</c>, the user will not be automatically redirected and will have to approve
        /// the app again.</param>
        /// <param name="disableSignup">When <c>true</c> (default is <c>false</c>) users will not be able to sign up for a
        /// Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox
        /// iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.</param>
        /// <returns>The uri of a web page which must be displayed to the user in order to authorize the app.</returns>
        public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string clientId, Uri redirectUri = null, string state = null, bool forceReapprove = false, bool disableSignup = false)
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("clientId");
            }
            else if (redirectUri == null && oauthResponseType != OAuthResponseType.Code)
            {
                throw new ArgumentNullException("redirectUri");
            }

            var dict = new Dictionary<string, string>();

            var queryBuilder = new StringBuilder();

            queryBuilder.Append("response_type=");
            switch (oauthResponseType)
            {
                case OAuthResponseType.Token:
                    queryBuilder.Append("token");
                    break;
                case OAuthResponseType.Code:
                    queryBuilder.Append("code");
                    break;
                default:
                    throw new ArgumentOutOfRangeException("oauthResponseType");
            }

            queryBuilder.Append("&client_id=").Append(Uri.EscapeDataString(clientId));

            if (redirectUri != null)
            {
                queryBuilder.Append("&redirect_uri=").Append(Uri.EscapeDataString(redirectUri.ToString()));
            }

            if (!string.IsNullOrWhiteSpace(state))
            {
                queryBuilder.Append("&state=").Append(Uri.EscapeDataString(state));
            }

            if (forceReapprove)
            {
                queryBuilder.Append("&force_reapprove=true");
            }

            if (disableSignup)
            {
                queryBuilder.Append("&disable_signup=true");
            }

            var uriBuilder = new UriBuilder("https://www.dropbox.com/1/oauth2/authorize");
            uriBuilder.Query = queryBuilder.ToString();

            return uriBuilder.Uri;
        }