web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Dynamics NAV .NET Class Library Store Certificate Not Found

(0) ShareShare
ReportReport
Posted on by

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];
}

*This post is locked for comments

I have the same question (0)
  • Guillem Padilla Profile Picture
    427 on at

    Hi,

    Can you share your NAV Code?

    Thanks

  • Community Member Profile Picture
    on at

    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..

  • Suggested answer
    Guillem Padilla Profile Picture
    427 on at

    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
    on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans