/// <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))); }
/// <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); }
/// <summary> /// Request a access token by exchanging a auth code. /// </summary> /// <param name="provider">OAuth2 provider wrapper.</param> /// <param name="redirectUri">URI to redirect back to the system.</param> /// <param name="code">Authorization code.</param> /// <returns>Authentication response object.</returns> public static OAuth2AuthenticateResponse AuthenticateByCode(OAuth2Provider provider, string redirectUri, string code) { var parameters = new Dictionary <string, string> { { "client_id", provider.ClientId }, { "client_secret", provider.ClientSecret }, { "redirect_uri", redirectUri }, { "code", code }, { "grant_type", "authorization_code" } }; 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)); }