示例#1
0
        /// <summary>
        /// Verifies the authorization results for the specified request identifier and the code of the authorization, and removes the request from memory.
        /// </summary>
        /// <param name="requestId">Request identifier.</param>
        /// <param name="code">The authorization code received from the provider server.</param>
        /// <returns>
        /// <para>Returns the verification results.</para>
        /// </returns>
        /// <remarks>
        /// <para>This method is intended for internal use. It is recommended to use the overload <see cref="VerifyAuthorization()"/> or <see cref="VerifyAuthorization(string)"/>.</para>
        /// </remarks>
        public static AuthorizationResult VerifyAuthorizationAndRemoveRequest(string requestId, string code)
        {
            var result = OAuthWeb.VerifyAuthorization(requestId, code);

            OAuthManager.RemoveRequest(requestId);
            return(result);
        }
示例#2
0
 /// <summary>
 /// Verifies the authorization results for the current URL.
 /// </summary>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use the overloads <see cref="VerifyAuthorization(string)"/> or <see cref="VerifyAuthorization(string, string)"/>.</para>
 /// </remarks>
 /// <returns>
 /// <para>Returns the verification results.</para>
 /// </returns>
 public static AuthorizationResult VerifyAuthorization()
 {
     if (HttpContext.Current == null)
     {
         return(new AuthorizationResult {
             ErrorInfo = new NullHttpContextException()
         });
     }
     return(OAuthWeb.VerifyAuthorization(HttpContext.Current.Request.Url.ToString()));
 }
示例#3
0
        /// <summary>
        /// Verifies the authorization results for the specified URL.
        /// </summary>
        /// <param name="url">Address at which to perform the verification.</param>
        /// <returns>
        /// <para>Returns the verification results.</para>
        /// </returns>
        public static AuthorizationResult VerifyAuthorization(string url)
        {
            var result = new AuthorizationResult();

            try
            {
                // HtmlDecode - small fix for wrong data from provider.
                // Thanks to @Nacer ( https://github.com/Nacer- ) // v1.8
                UriBuilder          u  = new UriBuilder(HttpUtility.HtmlDecode(url));
                NameValueCollection qs = null;

                if (!String.IsNullOrEmpty(u.Query))
                {
                    qs = HttpUtility.ParseQueryString(u.Query);
                }
                else if (String.IsNullOrEmpty(u.Query) && !String.IsNullOrEmpty(u.Fragment))
                {
                    qs = HttpUtility.ParseQueryString(u.Fragment.Substring(1));
                }

                if (qs == null)
                {
                    throw new AuthorizationException("Invalid URL. Verification code is not found.");
                }

                if (!String.IsNullOrEmpty(qs["denied"]))
                {
                    throw new AccessDeniedException(qs["error_description"]);
                }

                if (!String.IsNullOrEmpty(qs["error"]))
                {
                    switch (qs["error"].ToLower())
                    {
                    case "access_denied":
                        throw new AccessDeniedException(qs["error_description"]);

                    default:
                        throw new AuthorizationException(qs["error_description"] ?? qs["error"]);
                    }
                }

                result = OAuthWeb.VerifyAuthorization(qs["state"], qs["oauth_verifier"] ?? qs["code"]);
            }
            catch (Exception ex)
            {
                result.ErrorInfo = ex;
            }

            return(result);
        }
示例#4
0
        /// <summary>
        /// Verifies the authorization results for the specified URL.
        /// </summary>
        /// <param name="url">Address at which to perform the verification.</param>
        /// <returns>
        /// <para>Returns the verification results.</para>
        /// </returns>
        public static AuthorizationResult VerifyAuthorization(string url)
        {
            AuthorizationResult result = new AuthorizationResult();

            try
            {
                UriBuilder          u  = new UriBuilder(url);
                NameValueCollection qs = null;

                if (!String.IsNullOrEmpty(u.Query))
                {
                    qs = HttpUtility.ParseQueryString(u.Query);
                }
                else if (String.IsNullOrEmpty(u.Query) && !String.IsNullOrEmpty(u.Fragment))
                {
                    qs = HttpUtility.ParseQueryString(u.Fragment.Substring(1));
                }

                if (qs == null)
                {
                    throw new AuthorizationException("Invalid URL. Verification code is not found.");
                }

                if (!String.IsNullOrEmpty(qs["denied"]))
                {
                    throw new AccessDeniedException(qs["error_description"]);
                }
                if (!String.IsNullOrEmpty(qs["error"]))
                {
                    switch (qs["error"].ToLower())
                    {
                    case "access_denied":
                        throw new AccessDeniedException(qs["error_description"]);

                    default:
                        throw new AuthorizationException(qs["error_description"] ?? qs["error"]);
                    }
                }

                result = OAuthWeb.VerifyAuthorization(qs["state"], qs["oauth_verifier"] ?? qs["code"]);
            }
            catch (Exception ex)
            {
                result.ErrorInfo = ex;
            }

            return(result);
        }
示例#5
0
 /// <summary>
 /// Returns the authorization URL of the specified provider, query parameters and return URL.
 /// </summary>
 /// <param name="clientName">
 /// The provider name, through which it is necessary to authorize the current user; or the name of the registered client.
 /// </param>
 /// <param name="parameters">Additional parameters to be passed to the authorization URL.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(ClientName clientName, NameValueCollection parameters, string returnUrl)
 {
     return(OAuthWeb.GetAuthorizationUrl(clientName, parameters, returnUrl, null));
 }
示例#6
0
 /// <summary>
 /// Returns the authorization URL of the specified provider and return URL.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <param name="state">Custom state associated with authorization request.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(string clientName, string returnUrl, object state)
 {
     return(OAuthWeb.GetAuthorizationUrl(ClientName.Parse(clientName), null, returnUrl, state));
 }
示例#7
0
 /// <summary>
 /// Returns the authorization URL of the specified provider with specified parameters.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="parameters">Additional parameters to be passed to the authorization URL.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(string clientName, NameValueCollection parameters)
 {
     return(OAuthWeb.GetAuthorizationUrl(ClientName.Parse(clientName), parameters, null, null));
 }
示例#8
0
 /// <summary>
 /// Returns the authorization URL of the specified provider.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(string clientName)
 {
     return(OAuthWeb.GetAuthorizationUrl(ClientName.Parse(clientName), null, null, null));
 }
示例#9
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider with specified parameters.
 /// </summary>
 /// <param name="providerName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="parameters">Additional parameters to be passed to the authorization query.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="providerName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string, NameValueCollection)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string, NameValueCollection)"/>
 public static void RedirectToAuthorization(string providerName, NameValueCollection parameters)
 {
     OAuthWeb.RedirectToAuthorization(providerName, parameters, null);
 }
示例#10
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider and return URL.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <param name="state">Custom state associated with authorization request.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="clientName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string, string)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string, string, object)"/>
 public static void RedirectToAuthorization(string clientName, string returnUrl, object state)
 {
     OAuthWeb.RedirectToAuthorization(ClientName.Parse(clientName), null, returnUrl, state);
 }
示例#11
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider with specified parameters.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="parameters">Additional parameters to be passed to the authorization query.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="clientName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string, NameValueCollection)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string, NameValueCollection)"/>
 public static void RedirectToAuthorization(string clientName, NameValueCollection parameters)
 {
     OAuthWeb.RedirectToAuthorization(ClientName.Parse(clientName), parameters, null, null);
 }
示例#12
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="clientName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string)"/>
 public static void RedirectToAuthorization(string clientName)
 {
     OAuthWeb.RedirectToAuthorization(ClientName.Parse(clientName), null, null, null);
 }
示例#13
0
 /// <summary>
 /// Returns the authorization URL of the specified provider and return URL.
 /// </summary>
 /// <param name="providerName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(string providerName, string returnUrl)
 {
     return(OAuthWeb.GetAuthorizationUrl(providerName, null, returnUrl));
 }
示例#14
0
 /// <summary>
 /// Returns the authorization URL of the specified provider with specified parameters.
 /// </summary>
 /// <param name="providerName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="parameters">Additional parameters to be passed to the authorization URL.</param>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 public static string GetAuthorizationUrl(string providerName, NameValueCollection parameters)
 {
     return(OAuthWeb.GetAuthorizationUrl(providerName, parameters, null));
 }
示例#15
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider and return URL.
 /// </summary>
 /// <param name="providerName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="providerName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string, string)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string, string)"/>
 public static void RedirectToAuthorization(string providerName, string returnUrl)
 {
     OAuthWeb.RedirectToAuthorization(providerName, null, returnUrl);
 }
示例#16
0
 /// <summary>
 /// Verifies the authorization results for the current URL and removes the request from memory.
 /// </summary>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use the overloads <see cref="VerifyAuthorization(string)"/> or <see cref="VerifyAuthorization(string, string)"/>.</para>
 /// </remarks>
 /// <returns>
 /// <para>Returns the verification results.</para>
 /// </returns>
 public static AuthorizationResult VerifyAuthorizationAndRemoveRequest()
 {
     return(OAuthWeb.VerifyAuthorizationAndRemoveRequest(HttpContext.Current.Request.Url.ToString()));
 }
示例#17
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider, query parameters and return URL.
 /// </summary>
 /// <param name="clientName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
 /// <param name="parameters">Additional parameters to be passed to the authorization query.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="clientName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string, NameValueCollection, string)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(ClientName, NameValueCollection, string)"/>
 public static void RedirectToAuthorization(ClientName clientName, NameValueCollection parameters, string returnUrl)
 {
     OAuthWeb.RedirectToAuthorization(clientName, parameters, returnUrl, null);
 }
示例#18
0
 /// <summary>
 /// Redirects current client to the authorization page of the specified provider.
 /// </summary>
 /// <param name="providerName">Provider name, through which it is necessary to authorize the current user.</param>
 /// <exception cref="ClientIsNotRegisteredException">
 /// <paramref name="providerName"/> is unregistered. Use the <see cref="OAuthManager.RegisterClient(OAuthBase)" /> for OAuth clients registration.
 /// </exception>
 /// <exception cref="NullHttpContextException">
 /// The exception that is thrown when you try to access methods that are designed for web projects.
 /// </exception>
 /// <remarks>
 /// <para>The method will not work in desktop applications. For desktop applications you can use <see cref="GetAuthorizationUrl(string)"/>.</para>
 /// </remarks>
 /// <seealso cref="GetAuthorizationUrl(string)"/>
 public static void RedirectToAuthorization(string providerName)
 {
     OAuthWeb.RedirectToAuthorization(providerName, null, null);
 }