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];
}
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
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
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..
Hi,
Can you share your NAV Code?
Thanks
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,524 Super User 2024 Season 2
Martin Dráb 228,493 Most Valuable Professional
nmaenpaa 101,148