/// <summary> /// Create an HTTP Handler that supports OAuth user authentication. /// </summary> /// <param name="authBaseUri">The base auth URI e.g. https://auth.alitudeangel.com</param> /// <param name="clientId">Your client ID</param> /// <param name="clientSecret">Your client secret</param> /// <param name="scopes">Requested scopes</param> /// <param name="existingState">(optional) An existing state object from a previous session. May be null.</param> /// <param name="requireUserToken">true to aquire a user token, false to get a client only token.</param> /// <param name="redirectUri">The redirect URI to use for user token auth. Must match the registered URI for your client ID.</param> /// <param name="codeProvider">Implementation to use to get an authorization code URI from an auth login URI.</param> /// <returns> /// A <see cref="ClientHandlerInfo"/> object that contains the auth state and the handler. The auth state may be persisted and passed /// back in on future runs of the application to save login state. /// </returns> public static ClientHandlerInfo Create( string authBaseUri, string clientId, string clientSecret, IEnumerable<string> scopes, AuthorizationState existingState, bool requireUserToken, string redirectUri, IAuthorizeCodeProvider codeProvider) { AuthorizationServerDescription serverDescription = GetServerDescription(authBaseUri); ClientBase client; IAuthorizationState state = existingState; if (requireUserToken) { if (codeProvider == null || string.IsNullOrEmpty(redirectUri)) { throw new ArgumentNullException(nameof(codeProvider), $"{nameof(codeProvider)} or {nameof(redirectUri)} cannot be null if {nameof(requireUserToken)} is true."); } var userClient = new UserAgentClient(serverDescription, clientId, ClientCredentialApplicator.PostParameter(clientSecret)); if (state == null) { // Open browser here var returnTo = new Uri(redirectUri); Uri uri = userClient.RequestUserAuthorization(scopes, returnTo: returnTo); Uri result = codeProvider.GetCodeUri(uri, returnTo).Result; state = new AuthorizationState {Callback = returnTo}; state.Scope.AddRange(scopes); state = userClient.ProcessUserAuthorization(result, state); } client = userClient; } else { client = new WebServerClient(serverDescription, clientId, ClientCredentialApplicator.PostParameter(clientSecret)); state = state ?? client.GetClientAccessToken(scopes); } return new ClientHandlerInfo(client.CreateAuthorizingHandler(state), state); }
public Uri GetAuthorizationUri() { _endPoint = "https://start.exactonline.com"; _authorization = new AuthorizationState { Callback = new Uri("http://localhost:19648/home/exoauth2") }; var serverDescription = new AuthorizationServerDescription { AuthorizationEndpoint = new Uri(string.Format("{0}/api/oauth2/auth", _endPoint)), TokenEndpoint = new Uri(string.Format("{0}/api/oauth2/token", _endPoint)) }; _oAuthClient = new UserAgentClient(serverDescription, "eb93389b-9532-46ca-9b24-898a38e91c22", "jYzWXVFiE87C"); _oAuthClient.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("jYzWXVFiE87C"); return _oAuthClient.RequestUserAuthorization(_authorization); }
public string RetrieveAuthorization(UserAgentClient client, IAuthorizationState authorizationState) { if (!HttpListener.IsSupported) { throw new NotSupportedException("HttpListener is not supported by this platform."); } // Create a HttpListener for the specified url. //string url = string.Format(LoopbackCallback, GetRandomUnusedPort(), Util.ApplicationName); string url = string.Format(LoopbackCallback, GetRandomUnusedPort()); authorizationState.Callback = new Uri(url); var webserver = new HttpListener(); webserver.Prefixes.Add(url); // Retrieve the authorization url. Uri authUrl = client.RequestUserAuthorization(authorizationState); try { // Start the webserver. webserver.Start(); // Open the browser. Process.Start(authUrl.ToString() + "&access_type=offline&approval_prompt=force"); // Wait for the incoming connection, then handle the request. return HandleRequest(webserver.GetContext()); } catch (HttpListenerException ex) { throw new NotSupportedException("The HttpListener threw an exception.", ex); } finally { // Stop the server after handling the one request. webserver.Stop(); } }
public string RetrieveAuthorization(UserAgentClient client, IAuthorizationState authorizationState) { // Create the Url. authorizationState.Callback = new Uri(OutOfBandCallback); Uri url = client.RequestUserAuthorization(authorizationState); // Show the dialog. if (!Application.RenderWithVisualStyles) { Application.EnableVisualStyles(); } Application.DoEvents(); string authCode = OAuth2AuthorizationDialog.ShowDialog(url); Application.DoEvents(); if (string.IsNullOrEmpty(authCode)) { return null; // User cancelled the request. } return authCode; }
public static Uri GetAuthorization(UserAgentClient appClient) { return appClient.RequestUserAuthorization(GetState()); }