5

Sitecore 9.1 .NetCore 2.1 .Net 4.7.1

Below you will find the hello world console app code to talk to xConnect. You pass it a thumbprint and a url and it will try to initialize a connection. The only issue is that with a normal old school console app I connect fine. But in a Core app I get the dreaded The HTTP response was not successful: Unauthorized error. Exact same code. Both in the same admin command prompt.

I run the windows as consoleapp.exe I run the .netcore as dotnet coreconsoleapp.dll

I would not think so, but does core run in an alternate context? If I disable the cert in xConnect, the connect works. So it is definitely that core cannot process the cert properly. I have added code where I reach in the certificate registry and pull the cert, to test permissions and receive no errors on access.

class Program
{
    private static void Main(string[] args)
    {
        MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("");
        Console.WriteLine("END OF PROGRAM.");
        Console.ReadKey();
    }

    private static async Task MainAsync(string[] args)
    {
        CertificateHttpClientHandlerModifierOptions options =
        CertificateHttpClientHandlerModifierOptions.Parse("StoreName=My;StoreLocation=LocalMachine;FindType=FindByThumbprint;FindValue=DC7E10DC79E4BA82B5464BA6D56EED3F11CE7692");

        var certificateModifier = new CertificateHttpClientHandlerModifier(options);

        List<IHttpClientModifier> clientModifiers = new List<IHttpClientModifier>();
        var timeoutClientModifier = new TimeoutHttpClientModifier(new TimeSpan(0, 0, 20));
        clientModifiers.Add(timeoutClientModifier);

        var collectionClient = new CollectionWebApiClient(new Uri("https://xconnect/odata"), clientModifiers, new[] { certificateModifier });
        var searchClient = new SearchWebApiClient(new Uri("https://xconnect/odata"), clientModifiers, new[] { certificateModifier });
        var configurationClient = new ConfigurationWebApiClient(new Uri("https://xconnect/configuration"), clientModifiers, new[] { certificateModifier });

        var cfg = new XConnectClientConfiguration(
            new XdbRuntimeModel(CollectionModel.Model), collectionClient, searchClient, configurationClient);

        try
        {
            await cfg.InitializeAsync();

             // Print xConnect if configuration is valid
             var arr = new[]
            {
                    @"            ______                                                       __     ",
                    @"           /      \                                                     |  \    ",
                    @" __    __ |  $$$$$$\  ______   _______   _______    ______    _______  _| $$_   ",
                    @"|  \  /  \| $$   \$$ /      \ |       \ |       \  /      \  /       \|   $$ \  ",
                    @"\$$\/  $$| $$      |  $$$$$$\| $$$$$$$\| $$$$$$$\|  $$$$$$\|  $$$$$$$ \$$$$$$   ",
                    @" >$$  $$ | $$   __ | $$  | $$| $$  | $$| $$  | $$| $$    $$| $$        | $$ __  ",
                    @" /  $$$$\ | $$__/  \| $$__/ $$| $$  | $$| $$  | $$| $$$$$$$$| $$_____   | $$|  \",
                    @"|  $$ \$$\ \$$    $$ \$$    $$| $$  | $$| $$  | $$ \$$     \ \$$     \   \$$  $$",
                    @" \$$   \$$  \$$$$$$   \$$$$$$  \$$   \$$ \$$   \$$  \$$$$$$$  \$$$$$$$    \$$$$ "
                };
            Console.WindowWidth = 160;
            foreach (string line in arr)
                Console.WriteLine(line);

        }
        catch (XdbModelConflictException ce)
        {
            Console.WriteLine("ERROR:" + ce.Message);
            return;
        }

        // Initialize a client using the validated configuration
        using (var client = new XConnectClient(cfg))
        {
            try
            {

            }
            catch (XdbExecutionException ex)
            {
                // Deal with exception
            }
        }
    }
}
1
  • I think .net core 2.1 has a few extra conditions that need to be met (in comparison with framework) for the http stack to send the cert with the request. Does the client cert you want to use have appropriate "key usage" values? This issue report may be of interest: github.com/dotnet/corefx/issues/30483 Commented Jan 21, 2019 at 6:48

1 Answer 1

3

I got back info from Sitecore support on this.

.netCore has different requirements for approved certificates that .NET does. Core requires that the certificate have a "Key Usage" with a value or Digital Signature. You also have to locate this certificate to the personal certificate store on the local user. Not the local machine. This mean you have to have the cert in the Machine local store and Personal local store.

  1. Find a certificate that has key usage = Digital Signature.
  2. Copy the cert to Current User / Personal / Certificates store (this is for .NetCore)
  3. Copy the cert to Machine / Personal / Certificates store (this if for xConnect)
  4. Update the thumbprint in the xConnect /App_Config/AppSettings.config with this new certificate.
  5. Run your .NetCore app.

Alternately and not recommended, you can rem out the thumbprint setting in the xConnect /App_Config/AppSettings.config.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.