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);
        }
Beispiel #2
0
        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();
        }
        // returns the authentication token
        public string Run()
        {
            string accessToken = "";

            Console.WriteLine("--- Authorization---");
            Console.WriteLine("Authorization endpoint: " + clo.authorizationEndpoint);
            Console.WriteLine("Token endpoint: " + clo.tokenEndpoint);

            AuthorizationServerDescription authServerDescription = new AuthorizationServerDescription()
            {
                AuthorizationEndpoint = new Uri(clo.authorizationEndpoint),
                TokenEndpoint         = new Uri(clo.tokenEndpoint)
            };

            Console.WriteLine("Client ID: " + clo.clientId);
            Console.WriteLine("Client secret: ***");
            UserAgentClient client = new UserAgentClient(authServerDescription, clo.clientId, clo.clientSecret);

            Console.WriteLine("User: "******"Password: ***");
            List <string> scopes = new List <string>();

            scopes.Add("sessions.read");
            scopes.Add("sessions.write");
            scopes.Add("users.read");
            scopes.Add("users.write");
            scopes.Add("shoes.read");
            scopes.Add("shoes.write");
            scopes.Add("shoes.delete");
            scopes.Add("settings.read");
            IAuthorizationState authState = client.ExchangeUserCredentialForToken(clo.username, clo.password, scopes);

            if (authState != null && authState.AccessToken != null)
            {
                accessToken = authState.AccessToken;
                Console.WriteLine("Access token: " + accessToken);
            }

            Console.WriteLine("Success!");
            Console.WriteLine();

            return(accessToken);
        }
Beispiel #4
0
        private IAuthorizationState GetAccessTokenFromOwnAuthSvr()
        {
            var server = new AuthorizationServerDescription
            {
                TokenEndpoint   = new Uri(Config.ApiUrl + "/oauth/token"),
                ProtocolVersion = ProtocolVersion.V20
            };

            var client = new UserAgentClient(server)
            {
                //Those are the credentials for Downlords-FAF-Client
                ClientIdentifier           = Config.ClientIdentifier,
                ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(Config.ClientCredentialApplicator)
            };

            var token = client.ExchangeUserCredentialForToken(
                UsernameInputField.text, PasswordInputField.text);

            return(token);
        }