Getting Unique System ID on Mac using C#(Mono)

浪子不回头ぞ 提交于 2020-01-25 13:19:22

问题


Im porting a windows application for Mac using Mono.I cannot use DPAPI in Mac and i think there is restriction in getting Mainboard/CPU id in Mac for User Level programs.So is there something like DPAPI in windows which i can use using Mono to get a unique system id that does not change.


回答1:


First, stay away from using a MAC address as those can change (spoofed or hardware change) and is not a very Mac'ie way since 10.3/4?... Since then the "I/O Kit registry" is the preferred method to get a system id. Drop to a cmd line and man ioreg for same details.

So, not sure if you are using Xamarin.Mac or not, so if you are you could do a binding of the IOKit IORegistryEntryFromPath/IORegistryEntryCreateCFProperty APIs as I do not believe those are currently in C# wrappers, but really quick way that I use is:

using System;
using System.Diagnostics;
using System.Text;

namespace uuidmac
{
    class MainClass
    {
        static string MacUUID ()
        {
            var startInfo = new ProcessStartInfo () {
                FileName = "sh",
                Arguments = "-c \"ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/'\"",
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                UserName = Environment.UserName
            };
            var builder = new StringBuilder ();
            using (Process process = Process.Start (startInfo)) {
                process.WaitForExit ();
                builder.Append (process.StandardOutput.ReadToEnd ());
            }
            return builder.ToString ();
        }

        public static int Main (string[] args)
        {
            Console.WriteLine (MacUUID ());
            return 0;
        }
    }
}

Example output:

 "IOPlatformUUID" = "9E134619-9999-9999-9999-90DC147C61A9"

You could also parse out "IOPlatformSerialNumber" instead of the "IOPlatformUUID" if you want something human readable and that someone can find on the main dialog of "About This Mac" for tech support calls...

Now if you are looking at storing and protecting info as in the MS DPAPI, the KeyChain is the place to do that (OSX/iOS). Mono does have some of the DPAPI crypto apis that are x-plat based.



来源:https://stackoverflow.com/questions/31714965/getting-unique-system-id-on-mac-using-cmono

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!