Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Can't use System.Security.Cryptography.X509Certificates / System.Net.Sockets.TcpClient for WSA

Discussion in 'Windows' started by bboydaisuke, Mar 22, 2016.

  1. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    Hello,

    I'm porting my current project to WSA. I got these errors below on build.

    Assets\Script.cs(2,36): error CS0234: The type or namespace name 'X509Certificates' does not exist in the namespace 'System.Security.Cryptography' (are you missing an assembly reference?)
    Assets\Script.cs(6,5): error CS0246: The type or namespace name 'X509Certificate' could not be found (are you missing a using directive or an assembly reference?)
    Error building Player because scripts had compiler errors

    The Script.cs is as follows:

    using System.Collections;
    using System.Security.Cryptography.X509Certificates;

    public class Script
    {
    X509Certificate obj;
    }

    The error occurs only on build to Windows Store App. This code works fine for iOS, Android, Windows, etc.
    How can I build codes using System.Security.Cryptography.X509Certificates for Window Store App?

    thanks,
    Daisuke
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,671
    This is expected. Most of cryptography classes are not available in .NET Core. There usually is a similar replacements in Windows namespace, search for Window Store APIs.
     
  3. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    I see. I reviewed https://github.com/dotnet/corefx and System.Security.Cryptography.X509Certificates couldn't be found. Thank you for the response.

    That goes as well as System.Net.Sockets.TcpClient?
    The following code including TcpClient shows similar error:

    using System.Collections;
    using System.Net.Sockets;

    public class Script
    {
    TcpClient tcpClient;
    }

    Error:
    Assets\Script.cs(6,5): error CS0246: The type or namespace name 'TcpClient' could not be found (are you missing a using directive or an assembly reference?)

    However, I found .NET Core had TcpClient: https://github.com/dotnet/corefx/bl...t.Sockets/src/System/Net/Sockets/TCPClient.cs

    thanks,
    Daisuke
     
  4. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    I found .NET Core has X509Certificate too.
    .NET Core API reference has System.Security.Cryptography.X509Certificates as follows:


    Also found TcpClient in referece:


    Does Unity use different version of .NET Core than what I found?

    thanks,
    Daisuke
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,529
    Unity uses different .NET Core versions depending on the SDK you target. If you target Windows 8.1 or Windows Phone 8.1, we use .NET Core 4.5.1, while if you target Windows 10, we use .NET Core 5. I believe the source code on github is for .NET Core 5.
     
  6. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    It sounds that switching SDK to "Universal 10" solve the problem. However, It always fails to build with those errors whichever SDK selected.
     
  7. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    I may have to re-define the problem.

    [PROBLEM]
    Build to Windows Store App fails with errors when I use System.Security.Cryptography.X509Certificates / System.Net.Sockets.TcpClient.

    [ERROR]

    • Assets\Script.cs(2,36): error CS0234: The type or namespace name 'X509Certificates' does not exist in the namespace 'System.Security.Cryptography' (are you missing an assembly reference?)
    • Assets\Script.cs(7,5): error CS0246: The type or namespace name 'X509Certificate' could not be found (are you missing a using directive or an assembly reference?)
    • Assets\Script.cs(8,5): error CS0246: The type or namespace name 'TcpClient' could not be found (are you missing a using directive or an assembly reference?)

    [NOTES]
    I can build the project for Windows, Mac, Android, iOS, etc. It fails only when target is WSA.
    Unity uses .NET Core for WSA which has both X509Certificate and TcpClient.
    Switching SDK version (Universal 10, Universal 8.1, Phone 8.1, 8.1) does not solve the problem.

    [REPRO]
    Add this code to your project, switch target platform to WSA, and build.

    using System.Collections;
    using System.Security.Cryptography.X509Certificates;
    using System.Net.Sockets;

    public class Script
    {
    X509Certificate cert;
    TcpClient tcpClient;
    }
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,671
    When API is not available in SDK, you can try copying those from elsewhere/write your own, if that's easier than use replacement APIs.
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,529
    It might be that Windows Store uses a subset of what's available on .NET Core github. It's not really a bug in Unity.
     
  10. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    Indeed. I still see those errors when I tried to use those classes in Universal Windows app using Visual Studio without Unity. Anyway in the current situation you must rewrite your codes for WSA if your porting project uses TcpClient, X509Certificate, or maybe other classes.
     
  11. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    Update: I successfully built a small sample project which includes both TcpClient and X509Certificate to Windows Store App by switching Scripting Backend option from .NET to IL2CPP which is located at Other Settings in Windows Store Player settings. It works fine so far.

    notes: You have to generate App Manifest in Visual Studio, and configure it to allow network connection for network app.
     
    Last edited: Mar 25, 2016
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,671
    Just note, that with Il2Cpp you will not be able to use any newer .NET features or WinRT specific APIs, in case you need them. You are currently limited to what Mono allows you.

    Unity should generate manifest for you when building. You can find capabilities in Player Settings, then Unity will generate manifest with them set.
     
  13. bboydaisuke

    bboydaisuke

    Joined:
    Jun 14, 2014
    Posts:
    67
    I expect it works if I implement in-app purchase feature which uses Windows API (Windows Store API) within native plug-in for WSA. Or it won't work?
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,671
    It will work, but you will have to expose a C API out of that plugin and use it via P/Invoke.
    On WinRT you can write native plugins in a more convenient way, but we don't yet support that with Il2Cpp.
     
  15. MirkoSon

    MirkoSon

    Joined:
    Oct 14, 2015
    Posts:
    24
    Well ok, is there a solution to this? A .dll to include in the project or anything that would allow us to use this Windows namespace inside Unity? Sorry but it's not clear to me. Thanks
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,529
    Yeah. Just wrap that code in #if ENABLE_WINMD_SUPPORT/#endif defines. See more info here (this works on both .NET and IL2CPP scripting backends):

    https://docs.unity3d.com/Manual/IL2CPP-WindowsRuntimeSupport.html
     
  17. MirkoSon

    MirkoSon

    Joined:
    Oct 14, 2015
    Posts:
    24
    Thanks, but it still gives me the errors:

    error CS0234: The type or namespace name 'X509Certificates' does not exist in the namespace 'System.Security.Cryptography' (are you missing an assembly reference?)
    error CS0234: The type or namespace name 'Mail' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)

    Anything I can try? Is there a specific namespace for those certificates in Windows? I only can find reference to System. By the way, my project is for Hololens, so it's set for .Net and I don't think I have any custom WINMD or .dll to inspect with the Plugin Inspector the documentation is talking about...
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,529
    These namespaces don't exist on .NET scripting backend. Just using a define won't magically solve that - you need to rewrite your code. The define is there for using windows specific classes, which wouldn't compile in the editor.

    Alternatively, you could switch to IL2CPP scripting backend and have exact same API surface as the editor (including those namespaces you posted).
     
  19. MirkoSon

    MirkoSon

    Joined:
    Oct 14, 2015
    Posts:
    24