예제 #1
0
        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);
                }
            }
        }
예제 #2
0
 public static Authorization Create(
     AuthorizationParameters parameters,
     AuthorizationEnvironment environment,
     AuthorizationFlags flags)
 => Create(parameters?.ToAuthorizationRights(), environment, flags);