Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Signature for XML file

Posted on by 3,941

Hello Team. I really stuck in a rut with next problem:

  1. I generate XML file from NAV 2018.(web request)
  2. I have *.crt and *cer files (signed certificate)
  3. I need to generate signature for XML file using *.cer or *.crt file and I have no idea how to do it using NAV.

I even agree to use C/AL and dotnet variables in NAV2018 instead of AL and extensions if there is any way to generate signature (because i suppose that it's impossible to do without dotnet).

Any help will be appreciate.

*This post is locked for comments

  • JJMc Profile Picture
    JJMc 286 on at
    RE: Signature for XML file

    A question.

    And how or where are you setting the certificate and password to sign the document?

    Thank you ever so much

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    CSPParams DotNet System.Security.Cryptography.CspParameters.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    RSA DotNet System.Security.Cryptography.RSACryptoServiceProvider.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    XmlDocument DotNet System.Xml.XmlDocument.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    XmlTextReader DotNet System.Xml.XmlTextReader.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    SignedXml DotNet System.Security.Cryptography.Xml.SignedXml.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    Reference DotNet System.Security.Cryptography.Xml.Reference.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    Env DotNet System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform.'System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

    XmlDigSignature DotNet System.Xml.XmlElement.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

  • GreatScott001 Profile Picture
    GreatScott001 115 on at
    RE: Signature for XML file

    Can you list variable types?

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Signature for XML file

    Hi Andrey, I've checked and my addin performs criptography with signed certificate (with RSACryptoServiceProvider).

    I think this is not exactly what you was searching. Nice to see that you've solved your problem.

  • Verified answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    And the answer is:

    CSPParams := CSPParams.CspParameters(1, 'Microsoft Base Cryptographic Provider v1.0', 'DevRootCA');
    
    RSA := RSA.RSACryptoServiceProvider(CSPParams);
    
    XmlDocument := XmlDocument.XmlDocument;
    XmlDocument.PreserveWhitespace(FALSE);
    XmlDocument.Load(XmlTextReader.XmlTextReader('C:\Temp\Data.xml'));
    
    SignedXml := SignedXml.SignedXml(XmlDocument);
    SignedXml.SigningKey := RSA;
    
    Reference := Reference.Reference;
    Reference.Uri := '';
    
    Env := Env.XmlDsigEnvelopedSignatureTransform;
    Reference.AddTransform(Env);
    
    SignedXml.AddReference(Reference);
    
    SignedXml.ComputeSignature;
    XmlDigSignature := SignedXml.GetXml;
    MESSAGE(XmlDigSignature.InnerXml);


  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    So now i have next code:

    CSPParams := CSPParams.CspParameters;
    
    // Here is main issue ->
    //CSPParams.KeyContainerName = 'DevRootCA';
    //<-
    
    RSA := RSA.RSACryptoServiceProvider(CSPParams);
    
    XmlDocument := XmlDocument.XmlDocument;
    XmlDocument.PreserveWhitespace(FALSE);
    XmlDocument.Load(XmlTextReader.XmlTextReader('C:\Temp\Data.xml'));
    
    SignedXml := SignedXml.SignedXml(XmlDocument);
    SignedXml.SigningKey(RSA);
    
    Reference := Reference.Reference;
    Reference.Uri := '';
    
    Env := Env.XmlDsigEnvelopedSignatureTransform;
    Reference.AddTransform(Env);
    
    SignedXml.AddReference(Reference);
    
    SignedXml.ComputeSignature;
    XmlDigSignature := SignedXml.GetXml;
    MESSAGE(XmlDigSignature.InnerXml);


    I Have dotnet variable CSPParams which I took from mscorlib:

    System.Security.Cryptography.CspParameters.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    And the problem is that it doesn't have .KeyContainerName field. So I can't set it and get the key. As I understood - I just need to set SSL certificate name there? Has anyone encountered a similar problem?

    I'm moving through next instruction:

    docs.microsoft.com/.../how-to-sign-xml-documents-with-digital-signatures

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    Stefano Demiliani have you had a chance to look for code? I'll be very appreciate.

  • Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    It'll be great!

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Signature for XML file

    Yes if you want I can share it. I've only to return at my office for doing that :)

  • Verified answer
    Andrey Baludin Profile Picture
    Andrey Baludin 3,941 on at
    RE: Signature for XML file

    I found System.Security.Cryptography.Xml

    Thanks for inspiring me!

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,443 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans