The Content Hub C# SDK – Authentication

T

Forewarning, the code I am sharing is not going to be pretty, but it is going to prove out what you can do with the SDK and hopefully fill in some documentation gaps in the info provided by Sitecore. There are additional considerations that should be made for production projects including throttling. Make sure you have your user and OAuth Client configured.

//Use your content hub address (i.e. the address where you log in)
Uri endpoint = new Uri("https://your-instance-name.stylelabs.io");
            // These are the credentials for the user (UserName and Password) and the OAuth Client (ClientId and Client Secret)
            OAuthPasswordGrant oauth = new OAuthPasswordGrant
            {
                ClientId = "31b794b0-3849-4cfb-a596-eb1f58863503",
                ClientSecret = "EE5FCA20ECDDCDC07615628E51E6C31CEA9D77FFE248691E1C0A6E95B7616EBF",
                UserName = "integration-user",
                Password = "4v9P%5V$XdfpQuH"
            };

            // Create the Web SDK client with the endpoint URI and the OAuthPasswordGrant
            IWebMClient contentHubClient = MClientFactory.CreateMClient(endpoint, oauth);
            //The test connection method checks the client can successfully connect to the server, that the SDK and server are compatible and the credentials are correct
            await contentHubClient.TestConnectionAsync();

What can go wrong?

  • Your username or password are incorrect, or you don’t have a password – When I was reading the documentation I didn’t understand the process for creating a user. Remember, you need to create the username, set the email address and then reset the password to a value you know.
  • You’ve configured the user as an API user – Sounds like what you want right? Wrong. API users are there for legacy reasons and will not authenticate with an OAuth client.

About the author

Kelly Brennan
By Kelly Brennan

Your sidebar area is currently empty. Hurry up and add some widgets.