/// <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); }
/// <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())); }
/// <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); }
/// <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); }