Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            CredentialStore x = CredentialStoreFactory.CreateCredentialStore() as CredentialStore;

            char[] pass = { 'p', 'a', 's', 's' };
            x.AddPassword("Cred-2", Guid.NewGuid().ToString(), "User", pass);
        }
Ejemplo n.º 2
0
        public bool CreateNewUser()
        {
            try
            {
                cb.connect();
                ManagedObjectReference hostLocalAccountManager =
                    cb._connection._sic.accountManager;


                ManagedObjectReference hostAuthorizationManager =
                    cb._connection._sic.authorizationManager;

                String userName = GenerateRandomString();
                String password = GenerateRandomString();

                // Create an user
                HostAccountSpec hostAccountSpec = new HostAccountSpec();
                hostAccountSpec.id          = userName;
                hostAccountSpec.password    = password;
                hostAccountSpec.description = "User Description";
                cb._connection._service.CreateUser(hostLocalAccountManager,
                                                   hostAccountSpec);

                ManagedObjectReference rootFolder =
                    cb._connection._sic.rootFolder;

                Permission per = new Permission();
                per.group     = false;
                per.principal = userName;
                per.roleId    = -1;
                per.propagate = true;
                per.entity    = rootFolder;

                cb._connection._service.SetEntityPermissions(hostAuthorizationManager,
                                                             rootFolder,
                                                             new Permission[] { per });

                ICredentialStore csObj = CredentialStoreFactory.CreateCredentialStore();
                var createUserResult   = csObj.AddPassword(GetServerName(), userName, password.ToCharArray());
                if (createUserResult)
                {
                    Console.WriteLine("Successfully created user and populate the "
                                      + "credential store");
                    return(true);
                }

                return(false);
            }
            finally
            {
                cb.disConnect();
            }
        }
        /*Search Order for Username and Password
         * 1. Command Line Arguments
         * 2. Session File
         * 3. Enviroment Variable
         * 4. Credential Store
         * 5. Prompt the User for Username and Password
         */
        private void searchForUsernameAndPassword(Boolean flagUserName, Boolean flagPassword)
        {
            String username = null;
            String password = null;

            if (flagUserName && flagPassword)
            {
                return;
            }
            else if (optsEntered.ContainsKey("sessionfile"))
            {
                // Do Nothing
                return;
            }
            else
            {
                if (flagPassword == false)
                {
                    password = System.Environment.GetEnvironmentVariable("VI_Password");
                    if (password == null)
                    {
                        String host = null;
                        if (optsEntered.ContainsKey("server"))
                        {
                            foreach (DictionaryEntry uniEntry in optsEntered)
                            {
                                if (uniEntry.Key.Equals("server") && uniEntry.Value != null)
                                {
                                    host = uniEntry.Value.ToString();
                                }
                            }
                        }
                        else if (optsEntered.ContainsKey("url"))
                        {
                            String urlString = get_option("url");
                            foreach (DictionaryEntry uniEntry in optsEntered)
                            {
                                if (uniEntry.Key.Equals("url") && uniEntry.Value != null)
                                {
                                    urlString = uniEntry.Value.ToString();
                                }
                            }
                            if (urlString.IndexOf("https://") != -1)
                            {
                                int sind = 8;
                                int lind = urlString.IndexOf("/sdk");
                                host = urlString.Substring(sind, lind - 8);
                            }
                            else if (urlString.IndexOf("http://") != -1)
                            {
                                int sind = 7;
                                int lind = urlString.IndexOf("/sdk");
                                host = urlString.Substring(sind, lind - 7);
                            }
                            else
                            {
                                host = urlString;
                            }
                        }
                        else
                        {
                            // User Neds to Specify Either User Name aand Password
                        }
                        try
                        {
                            ICredentialStore csObj = CredentialStoreFactory.CreateCredentialStore();
                            //from here
                            int count = 0;
                            if (flagUserName == false)
                            {
                                username = System.Environment.GetEnvironmentVariable("VI_Username");
                                optsEntered["username"] = username;
                                if (username == null)
                                {
                                    IEnumerable userNameSet = csObj.GetUsernames(host);
                                    IEnumerator userNameIt  = userNameSet.GetEnumerator();

                                    while (userNameIt.MoveNext())
                                    {
                                        count++;
                                        username = userNameIt.Current.ToString();
                                        optsEntered["username"] = username;
                                        username = get_option("username");
                                    }
                                }
                                if (count > 1)
                                {
                                    username = readUserName();
                                    optsEntered["username"] = username;
                                    username = get_option("username");
                                }
                                flagUserName = true;
                            }
                            else
                            {
                                username = get_option("username");
                            }
                            // to here
                            char[] data = csObj.GetPassword(host, username);
                            password = new String(data);
                            optsEntered["password"] = password;
                        }
                        catch (Exception e)
                        {
                            // Do Noting
                            // Not able to find the password in Cred Store
                        }
                    }
                    else
                    {
                        optsEntered["password"] = password;
                    }
                    if (flagUserName == false)
                    {
                        username = System.Environment.GetEnvironmentVariable("VI_Username");
                        if (username == null)
                        {
                            // Read It From The Console
                            username = readUserName();
                        }
                        optsEntered["username"] = username;
                    }
                    if (password == null || password == "")
                    {
                        password = readPassword();
                        optsEntered["password"] = password;
                    }
                }
            }
        }