public static Authorization Create(
            AuthorizationRights rights,
            AuthorizationEnvironment environment,
            AuthorizationFlags flags)
        {
            NativeAuthorizationItemSet  rightsNative = new NativeAuthorizationItemSet();
            NativeAuthorizationItemSet *rightsPtr    = null;

            NativeAuthorizationItemSet  envNative = new NativeAuthorizationItemSet();
            NativeAuthorizationItemSet *envPtr    = null;

            int    code;
            IntPtr auth;

            try {
                if (rights != null && rights.Count > 0)
                {
                    rights.ToNative(ref rightsNative);
                    rightsPtr = &rightsNative;
                }

                if (environment != null)
                {
                    environment.ToAuthorizationItemSet().ToNative(ref envNative);
                    if (envNative.count > 0)
                    {
                        envPtr = &envNative;
                    }
                }

                code = AuthorizationCreate(rightsPtr, envPtr, flags, out auth);
                if (code != 0)
                {
                    return(null);
                }

                return(new Authorization(auth));
            } finally {
                AuthorizationItemSet.FreeNative(rightsPtr);
                AuthorizationItemSet.FreeNative(envPtr);
            }
        }
        internal AuthorizationItemSet ToAuthorizationItemSet()
        {
            // NOTE: these are all #define, not resolvable symbols
            const string kAuthorizationEnvironmentUsername = "******";
            const string kAuthorizationEnvironmentPassword = "******";
            const string kAuthorizationEnvironmentShared   = "shared";
            const string kAuthorizationEnvironmentPrompt   = "prompt";
            const string kAuthorizationEnvironmentIcon     = "icon";

            var items = new AuthorizationItemSet();

            if (Username != null)
            {
                items.Add(kAuthorizationEnvironmentUsername, Username);
            }

            if (Password != null)
            {
                items.Add(kAuthorizationEnvironmentPassword, Password);
            }

            if (AddToSharedCredentialPool)
            {
                items.Add(kAuthorizationEnvironmentShared);
            }

            if (Prompt != null)
            {
                items.Add(kAuthorizationEnvironmentPrompt, Prompt);
            }

            if (IconPath != null)
            {
                items.Add(kAuthorizationEnvironmentIcon, IconPath);
            }

            return(items);
        }
        public static Authorization Create(AuthorizationParameters parameters, AuthorizationEnvironment environment, AuthorizationFlags flags)
        {
            AuthorizationItemSet  pars   = new AuthorizationItemSet();
            AuthorizationItemSet *ppars  = null;
            AuthorizationItem *   pitems = null;
            AuthorizationItemSet  env    = new AuthorizationItemSet();
            AuthorizationItemSet *penv   = null;
            AuthorizationItem *   eitems = null;
            int    code;
            IntPtr auth;

            try {
                unsafe {
                    if (parameters != null)
                    {
                        ppars = &pars;
                        pars.ptrToAuthorization = (AuthorizationItem *)Marshal.AllocHGlobal(sizeof(AuthorizationItem) * 3);
                        if (parameters.PathToSystemPrivilegeTool != null)
                        {
                            EncodeString(ref pars.ptrToAuthorization [pars.count++], "system.privilege.admin", parameters.PathToSystemPrivilegeTool);
                        }
                        if (parameters.Prompt != null)
                        {
                            EncodeString(ref pars.ptrToAuthorization [pars.count++], "prompt", parameters.Prompt);
                        }
                        if (parameters.IconPath != null)
                        {
                            EncodeString(ref pars.ptrToAuthorization [pars.count++], "prompt", parameters.IconPath);
                        }
                    }
                    if (environment != null)
                    {
                        penv = &env;
                        env.ptrToAuthorization = (AuthorizationItem *)Marshal.AllocHGlobal(sizeof(AuthorizationItem) * 3);
                        if (environment.Username != null)
                        {
                            EncodeString(ref env.ptrToAuthorization [env.count++], "username", environment.Username);
                        }
                        if (environment.Password != null)
                        {
                            EncodeString(ref env.ptrToAuthorization [env.count++], "password", environment.Password);
                        }
                        if (environment.AddToSharedCredentialPool)
                        {
                            EncodeString(ref env.ptrToAuthorization [env.count++], "shared", null);
                        }
                    }
                    code = AuthorizationCreate(ppars, penv, flags, out auth);
                    if (code != 0)
                    {
                        return(null);
                    }
                    return(new Authorization(auth));
                }
            } finally {
                if (ppars != null)
                {
                    for (int i = 0; i < pars.count; i++)
                    {
                        Marshal.FreeHGlobal(pars.ptrToAuthorization [i].name);
                        Marshal.FreeHGlobal(pars.ptrToAuthorization [i].value);
                    }
                    Marshal.FreeHGlobal((IntPtr)pars.ptrToAuthorization);
                }
                if (penv != null)
                {
                    for (int i = 0; i < env.count; i++)
                    {
                        Marshal.FreeHGlobal(env.ptrToAuthorization [i].name);
                        if (env.ptrToAuthorization [i].value != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(env.ptrToAuthorization [i].value);
                        }
                    }
                    Marshal.FreeHGlobal((IntPtr)env.ptrToAuthorization);
                }
            }
        }