Skip to main content

Notifications

Microsoft Dynamics NAV forum
Suggested answer

Dynamics NAV .NET Class Library Store Certificate Not Found

Posted on by Microsoft Employee

Hi Experts,

I have an issue when calling my DLL Class Library from NAV, when it hits my GetClientCertificate it is not able to find any certificates. However, when I run this same code from a console app, it is able to find the certificate. Please refer below my code. What could be the issues?

public static X509Certificate2 GetClientCertificate()
{
var store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
return store.Certificates.Find(X509FindType.FindByThumbprint, "3c256a066036515eb6ac5a28ce11c0760b21163d", true)[0];
}

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics NAV .NET Class Library Store Certificate Not Found

    Are you running both on the same machine? Yes

    If you run this .NET code on client machine it works? Yes, as a Console App. It only doesnt work when called from C/AL

    Please, check if the Dynamics NAV Instance login is really the network service user Yes It is

    Also check if the user that runs this code in NAV have permissions to see certificates How do we do that?

    You can also use FQDN to specify the remote server StoreName (\\Yourserver\TrustedPublisher) Its local server, everything is on one computer, just for my dev purposes

    Check this possibilities

  • Suggested answer
    Guillem Padilla Profile Picture
    Guillem Padilla 425 on at
    RE: Dynamics NAV .NET Class Library Store Certificate Not Found

    Are you running both on the same machine?

    If you run this .NET code on client machine it works?

    Please, check if the Dynamics NAV Instance login is really the network service user

    Also check if the user that runs this code in NAV have permissions to see certificates

    You can also use FQDN to specify the remote server StoreName (\\Yourserver\TrustedPublisher)

    Check this possibilities

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics NAV .NET Class Library Store Certificate Not Found

    Hi, 

    Below is my C/AL Code

    //SignedInvoice : NAVIntegration.SignedInvoice.'NAVIntegration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

    //NavRequest: NAVIntegration.InvFiscalizationRequest.'NAVIntegration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

    SignedInvoice := NavRequest.SignInvoice();

    in .NET Standard 2.0 Class Library:

    public SignedInvoice SignInvoice()
    {
    HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(this), Encoding.UTF8, "application/json");

    HttpClient client;
    HttpClientHandler handler;
    GetClientAndHandler(out handler, out client);
    var response = client.PostAsync($"api/Sign/SignInvoice", httpContent).Result;
    if (response.StatusCode == HttpStatusCode.OK)
    {
    var jsonString = response.Content.ReadAsStringAsync();
    jsonString.Wait();
    var invoiceResponse = jsonString.Result;
    response.Content = new StringContent(invoiceResponse);

    SignedInvoice SignedResponse = JsonConvert.DeserializeObject<SignedInvoice>(invoiceResponse);
    return SignedResponse;
    }
    return new SignedInvoice();
    }

    static void GetClientAndHandler(out HttpClientHandler handler, out HttpClient client)
    {
    handler = CreateWebRequestHandler();
    client = new HttpClient(handler);
    client.BaseAddress = new Uri("(confidential, there is a web url here)");
    client.DefaultRequestHeaders.Accept.Clear();

    }
    public static HttpClientHandler CreateWebRequestHandler()
    {
    var handler = new HttpClientHandler();
    var cert = GetClientCertificate();
    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
    handler.ClientCertificates.Add(cert);
    return handler;
    }

    public static X509Certificate2 GetClientCertificate()
    {
    var store = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
    store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
    return store.Certificates.Find(X509FindType.FindByThumbprint, "3c256a066036515eb6ac5a28ce11c0760b21163d", true)[0];
    }

    Below is the Event Viewer Error

    Message <ii>(NavNCLDotNetInvokeException): <ii>A call to NAVIntegration.InvFiscalizationRequest.SignInvoice failed with this message: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    ExceptionStackTrace:
    at Microsoft.Dynamics.Nav.Runtime.NavDotNet.Invoke[T](String methodName, UInt32 methodIndex, BindingFlags flags, ParameterModifier modifier, Type[] referenceTypes, Object[] arguments)
    at Microsoft.Dynamics.Nav.Runtime.NavDotNet.InvokeMethod[T](Boolean isStatic, String methodName, UInt32 methodIndex, Object[] arguments)
    at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit99009692.OnRun_Scope.OnRun()
    at Filter.InvokeWithFilter(NavMethodScope )
    at Microsoft.Dynamics.Nav.Runtime.NavMethodScope.Run()
    at Microsoft.Dynamics.Nav.BusinessApplication.Codeunit99009692.OnRun(INavRecordHandle εrec)
    at Microsoft.Dynamics.Nav.Runtime.NavCodeunit.DoRun(DataError errorLevel, NavRecord record)
    at Microsoft.Dynamics.Nav.Runtime.NavCodeunit.InvokeRun(Int32 objectId, Object[] args)
    at Microsoft.Dynamics.Nav.Service.NSCodeUnit.RunCodeunit(CodeUnitResponse codeUnit)
    at Microsoft.Dynamics.Nav.Service.NSCodeUnit.Invoke()
    at Microsoft.Dynamics.Nav.Service.NSService.InvokeApplicationMethod(ApplicationMethodRequest applicationMethodRequest, NavRecordState state)
    at SyncInvokeInvokeApplicationMethod(Object , Object[] , Object[] )
    at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.RunInTransactionCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.TransientErrorRetryCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.ErrorMappingCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    CallerStackTrace:
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.ErrorMappingCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.PushPopCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.TraceScopeCombinator(Category telemetryCategory, ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass10_0.<PerformanceCounterCombinator>b__0()
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.PerformanceCounterCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.InitClientTelemetryIdsCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.TlsClearCombinator(ServiceOperation innerOperation, NSServiceBase serviceInstance, MethodBase syncMethod, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.<>c__DisplayClass25_0.<Combine>b__1(NSServiceBase serviceInstance, Object[] inputs, Object[]& outputs)
    at Microsoft.Dynamics.Nav.Service.ServiceOperationInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
    at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
    at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
    at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
    at System.ServiceModel.Dispatcher.MessageRpc.Wrapper.Resume(Boolean& alreadyResumedNoLock)
    at System.ServiceModel.Dispatcher.ThreadBehavior.ResumeProcessing(IResumeMessageRpc resume)
    at Microsoft.Dynamics.Nav.Runtime.NavSynchronizationContext.<>c__DisplayClass1_0.<ClearThreadLocalStorageDelegate>b__0(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()

    However, When I run this code from a Console App, It is able to find certificate.. Only when its called from NAV its not finding it.

    I have created cert in Dynamics NAV Server using mmc in all the places, as well as given full control to my network service user from the certificate..

  • Guillem Padilla Profile Picture
    Guillem Padilla 425 on at
    RE: Dynamics NAV .NET Class Library Store Certificate Not Found

    Hi,

    Can you share your NAV Code?

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans