Search Unity

Can't use System.Security.Cryptography.Xml

Discussion in 'Windows' started by Jayme65, Apr 5, 2017.

  1. Jayme65

    Jayme65

    Joined:
    Dec 11, 2016
    Posts:
    94
    Hello,

    I have to read a signed xml with a
    Code (csharp):
    1. Using System.Security;
    2. System.Security.Cryptography.Xml.SignedXml signedXml = new System.Security.Cryptography.Xml.SignedXml(lic);
    First Unity/VS complained that
    So I added a reference to System.Security in Visual Studio (C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Security.dll)...but Unity keeps complaining that

    I then tried to directly copy the 'System.Security.dll' inside the asset folder (thus removing it from VS references) but there I get a:
    What am I doing wrong please? How should I proceed?
    Thanks a lot!
     
    Last edited: Apr 6, 2017
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Create a text file named "mcs.rsp" in your project Assets folder that contains the following:

    Code (csharp):
    1. -r:System.Security.dll
     
  3. Jayme65

    Jayme65

    Joined:
    Dec 11, 2016
    Posts:
    94
    Thanks for the reply...I did it, restarted Unity and I get a "error CS0006: Metadata file `System.Security.dll' could not be found" message

    Any other idea?
     
    Last edited: Apr 6, 2017
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You need to use .NET 2.0 API compatibility level in player settings:

    upload_2017-4-6_10-53-18.png

    System.Security.dll is not available in .NET 2.0 Subset.
     
  5. Jayme65

    Jayme65

    Joined:
    Dec 11, 2016
    Posts:
    94
    Tautvydas-Zilys,

    Thanks so much for your reply! It now works!

    May I please ask you:
    - Each time I quit and launch VisualStudio I can see that the reference to 'System.Security.dll' is lost
    Why is it so?
    - Removing 'System.Security.dll' from the asset folder doesn't give me the 'missing assembly' error that I had from the beginning
    Does it mean that I don't need to include it anymore?
    - What's the meaning of this 'mcs.rsp' and what does '-r' stand for?

    In summary, all seems now to work with NO reference to the dll in VisualStudio and NO copy of the dll in the asset folder!

    Would you please help me understand what's happening here?

    Thanks again!!

    PS:
    How can I fix VisualStudio from removing reference to the dll and then complaining that 'The type or namespace name `Xml' does not exist in the namespace `System.Security.Cryptography'. Are you missing an assembly reference?'
    Do I have to worry about this or not?
     
    Last edited: Apr 6, 2017
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Unity doesn't look at what you change in VS project. It generates them every time you open unity or add or remove a script.

    You definitely don't need to include System.Security.dll into your project. Unity has its own copy in <EDITOR_INSTALL_DIR>\Data\Mono\lib\mono\2.0. It is not used by default, but "mcs.rsp" file contains a list of extra arguments that should be passed to the compiler. And that's what it contains: it tells it to reference System.Security.dll.

    I'll try to find out more info about VS use case.
     
  7. sailro

    sailro

    Microsoft

    Joined:
    Jul 30, 2014
    Posts:
    167
    Hi,

    We worked on improving the handling on mcs.rsp files, especially regarding references with Visual Studio.

    Feel free to send us an email at vstusp[at]microsoft[dot]com to have access to a preview build with this feature.

    Regards
    Sebastien Lebreton [MSFT]
     
    Peter77 likes this.
  8. Jayme65

    Jayme65

    Joined:
    Dec 11, 2016
    Posts:
    94
    Tautvydas-Zilys,

    Thank you for your reply and the time taken to help me!
    Thanks to your explanation this subject is now very clear to me!

    A big thank you, really!
     
    Last edited: Sep 7, 2017
  9. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Hi Tuatvydas,

    Unity 2017.2f3 with NET 4.6 backend ( iOS Build and Android Build )

    I put this into mcs.rps

    -r:System.Data.dll
    -r:System.Security.dll

    Trying to compile for iOS but I still get at the end of the exporting process

    Error building Player: DllNotFoundException: Security

    Is that broken win NET 4.6? I mean seems like System.Security is not added.

    Thanks, Marek.
     
  10. haswalt

    haswalt

    Joined:
    Oct 28, 2016
    Posts:
    2
    I also get the same using NET 4.6. Tried switching to newer version to work around SSL issues with SNI certificates but I can't get past the NotFoundException.

    I've added toe rsp file but it doesn't work, building for iOS and Android
     
  11. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    I, too, have this same problem. I'm using 2017.2f3 an NET 4.6.
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Can we get a bug report on that?
     
  13. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    I was just making repro case and found out that enabling performance reporting service causes this error. After disabling performance reporting, problem goes away. I filed bug report anyways though.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Thanks! Do you have a case #?
     
  15. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Nice catch with the performance reporting. Tautvydas when we can expect fix for that? Will it land in 2017.2?
     
  16. MasoInar

    MasoInar

    Joined:
    Feb 20, 2014
    Posts:
    126
    Case 963348
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Thanks for the bug report. Unfortunately I can't give you any ETAs on when that will be fixed.
     
    MasoInar likes this.
  18. Koara92

    Koara92

    Joined:
    Nov 9, 2014
    Posts:
    100
    Has this been fixed in recent unity versions? I'm still on 2017.3.1f1 and don't see much of a point of updating if it hasn't.
     
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Which specific issue are you seeing?
     
  20. Koara92

    Koara92

    Joined:
    Nov 9, 2014
    Posts:
    100
    I am trying to use the System.Security.Cryptography.ProtectedData class but have been running into many issues.

    Firstly, the ProtectedData class is not even located in the default System.Security.Cryptography namescape supplied by unity. So, I go to the Nuget package manager to download the System.Security.Cryptography.ProtectedData dll. After importing that, and placing it in my plugins folder, I still have an error, albeit a different one.

    This error states: "The type name 'ProtectedData' could not be found in the namespace 'System.Security.Cryptography'. This type has been forwarded to assembly 'System.Security, Version=4.0.0.0, Culture=neutral, PubliCKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly."

    So, I then delete the ProtectedData dll that I placed in the plugins folder, and place the System.Security dll in the plugins folder. After doing that, I get an error stating that it failed to load the assembly System.Security.dll.

    I have tried adding the text file with text "-r:System.Security.dll" but that does not fix any of these issues either.

    I am using 2017.3.1f1 right now, and would like to stay with this version if it is possible to fix this issue. Is there some fix for this version, or would I have to upgrade to the latest version? Is there even a fix for this in Unity 2018 as of yet?
     
  21. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Where did you take System.Security.dll from? What did you name the file that contains "-r:System.Security.dll"? Which platform are you targeting?
     
  22. kayy

    kayy

    Joined:
    Jul 26, 2011
    Posts:
    110
    I ran into the same problem while working with 2018.2.18, API Compatibility Level 4.x. I created an mcs.rsp and csc.rsp and added -r:System.Security.dll to both of them so that I can build for iOS and UWP/HoloLens (scripting backend still .NET).
    On both targets I get this mcs/csc.rsp: not parsed correctly: System.Security.dll could not be found as a system library.
    • Is 4.x supported for all platforms?
    • Do I need to copy the DLLs to some location under my project's Assets dir?
      As far as I understand it, the response file will manage this automatically. If so: Should it be Plugins, Plugins/iOS or is any folder OK.
    • Does an update to 2018.3 or 2019 solve this?
    PS: I don't use performance reporting. My OSs are Win 10 Pro 1803 and macOS High Sierra
     
  23. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    This would work with mcs.rsp - are you sure that's the error you are getting? For csc.rsp - I don't think .NET scripting backend even has System.Security.dll.
     
  24. kayy

    kayy

    Joined:
    Jul 26, 2011
    Posts:
    110
    OK that means IL2CPP should work fine. We planned to migrate but we had some issues with NewtonSoft etc. related to API compatibility level 4.x which should be fixed in 2018.3.
    Regarding iOS I don't have access to my Mac today but will check this ASAP.

    The good news is I was able to build for Android without errors. Thanks anyway I will post an update :)
     
  25. kayy

    kayy

    Joined:
    Jul 26, 2011
    Posts:
    110
    Finally I worked out the problem. In short:
    You have to set Api Compatibility Level to .NET 4x for IL2CPP. If in trouble because of other frameworks that are only available for .NET Standard 2.0, check if this has been solved in Unity 2018.3 and try switching to .NET 4.x.

    Longer version:
    • iOS refused to work due to our player setting of the Api Compatibility Level .NET Standard 2.0. This is necessary under Unity 2018.2 if XmlSerializer is used. Otherwise runtime errors ar thrown because System.Reflection.Emit namespace is not allowed. This was fixed in 2018.3 with AOT-friendly class library implementation (as announced by Josh Peterson) so that .NET 4.x can now be used.
    • Android works fine with -r:System.Security.dll entry in mcs.rsp as we use Mono as Scripting Backend (I guess IL2CPP would cause the same situation as under iOS
    • UWP/HoloLens with Scripting Backend .NET instead of IL2CPP is not supported as @Tautvydas-Zilys has stated. So IL2CPP should do but again will work only with 2018.3 because of XmlSerialiser.
      I have not yet updated to 2018.3 under Windows and thus not tested it, but I am pretty optimistic.