private static void Main() { // DotNetOpenAuth only issues access tokens when the client uses an HTTPS connection. As we will most // likely run the server on our local development machine with only a self-signed SSL certificate, setting up // connection to the server will fail as the SSL certificate is considered invalid by the .NET framework. // To circumvent this, we add the line below that will consider all SSL certificates as valid, including // self-signed certificaties. Note: this should only be used for testing purposes. ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; // The description of the authorization server to which we will be connecting. The most important component // is the token endpoint, which is the URL at which the server listens for token requests var authorizationServerDescription = new AuthorizationServerDescription { TokenEndpoint = new Uri("https://localhost:44303/tokens"), ProtocolVersion = ProtocolVersion.V20 }; // Create the client with which we will be connecting to the server. var userAgentClient = new UserAgentClient(authorizationServerDescription, clientIdentifier: "demo-client-1", clientSecret: "demo-client-secret-1"); // The scope that we request for the client. Note: this can also be null if we don't want to request any specific // scope or more than one scope if we want to request an access token that is valid for several scopes var clientScopes = new[] { "demo-scope-client-1" }; // Request a new client access token for the specified scopes (http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.4) // This method will use the client identifier and client secret used when constructing the UserAgentClient instance var clientAccessToken = userAgentClient.GetClientAccessToken(clientScopes); // Output some information about the retrieved client access token Console.WriteLine("[RETRIEVED CLIENT ACCESS TOKEN]"); Console.WriteLine("Access token: {0}", clientAccessToken.AccessToken); Console.WriteLine("Expiration time: {0}", clientAccessToken.AccessTokenExpirationUtc); Console.WriteLine("Scope: {0}", OAuthUtilities.JoinScopes(clientAccessToken.Scope)); // The scope that we request for the user. Note: this can also be null if we don't want to request any specific // scope or more than one scope if we want to request an access token that is valid for several scopes var userScopes = new[] { "demo-scope-1" }; // Request a new user access token for the specified user and the specified scopes (http://tools.ietf.org/html/draft-ietf-oauth-v2-31#page-35) var userAccessToken = userAgentClient.ExchangeUserCredentialForToken("demo-user-1", "demo-user-password-1", userScopes); // Output some information about the retrieved user access token Console.WriteLine("\n[RETRIEVED USER ACCESS TOKEN]"); Console.WriteLine("Access token: {0}", userAccessToken.AccessToken); Console.WriteLine("Refresh token: {0}", userAccessToken.RefreshToken); Console.WriteLine("Expiration time: {0}", userAccessToken.AccessTokenExpirationUtc); Console.WriteLine("Scope: {0}", OAuthUtilities.JoinScopes(userAccessToken.Scope)); var refreshed = userAgentClient.RefreshAuthorization(userAccessToken); Console.WriteLine("\n[REFRESHING USER ACCESS TOKEN]"); Console.WriteLine("Access token refreshed: {0}", refreshed); Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); }
private IAuthorizationState GetAccessToken(string refresh) { var authorizationServer = new AuthorizationServerDescription { TokenEndpoint = new Uri(this.TokenUri), ProtocolVersion = ProtocolVersion.V20, }; var client = new UserAgentClient(authorizationServer, this.ClientId, this.ClientSecret); IAuthorizationState ostate; if (refresh == null) ostate = client.GetClientAccessToken(new[] { this.API_EndpointUri }); else { ostate = new AuthorizationState(new[] { this.API_EndpointUri }); ostate.RefreshToken = refresh; client.RefreshAuthorization(ostate); } return ostate; }
private static IAuthorizationState GetAccessToken(string refresh) { var authorizationServer = new AuthorizationServerDescription { //AuthorizationEndpoint = new Uri(AUTHORIZATION_ENDPOINT), TokenEndpoint = new Uri(TOKEN_ENDPOINT), ProtocolVersion = ProtocolVersion.V20, }; // get a reference to the auth server var client = new UserAgentClient(authorizationServer, CLIENT_ID, CLIENT_SECRET); // now get a token IAuthorizationState ostate; if (refresh == null) ostate = client.ExchangeUserCredentialForToken(TEST_USERNAME, TEST_PASSWORD, new[] { API_ENDPOINT }); else { // we had previously authenticated so we can use the token rather than the credentials to get a new access token ostate = new AuthorizationState(new[] { API_ENDPOINT }); ostate.RefreshToken = refresh; client.RefreshAuthorization(ostate); } // return result return ostate; }
private static IAuthorizationState GetAccessToken(string refresh) { var authorizationServer = new AuthorizationServerDescription { TokenEndpoint = new Uri(TokenEndpoint), ProtocolVersion = ProtocolVersion.V20, }; // get a reference to the auth server var client = new UserAgentClient(authorizationServer, ClientId, ClientSecret); // now get a token IAuthorizationState authorizationState; if (refresh == null) { authorizationState = client.ExchangeUserCredentialForToken(TestUsername, TestPassword, new[] { ApiEndpoint }); } else { // we had previously authenticated so we can use the token rather than the credentials to get a new access token authorizationState = new AuthorizationState(new[] { ApiEndpoint }) { RefreshToken = refresh }; client.RefreshAuthorization(authorizationState); } // return result return authorizationState; }