Opc.Ua.Configuration.AccountInfo.Create C# (CSharp) Метод

Create() публичный статический Метод

Creates an account info object from an identity name.
public static Create ( string identityName ) : AccountInfo
identityName string
Результат AccountInfo
        public static AccountInfo Create(string identityName)
        {
            Console.WriteLine("CONFIGURATION CONSOLE AccountInfo {0}", identityName);

            // check for null.
            if (String.IsNullOrEmpty(identityName))
            {
                return null;
            }

            StringBuilder builder = new StringBuilder();

            // check for SID based query.
            if (identityName.StartsWith("S-"))
            {
                builder.AppendFormat("SID='{0}'", identityName);
            }

            // search by account name.
            else
            {
                string domain = null;
                string name = identityName;

                int index = identityName.IndexOf('\\');

                if (index != -1)
                {
                    domain = identityName.Substring(0, index);
                    name = identityName.Substring(index+1);
                }
           
                builder.AppendFormat("Name='{0}'", name);

                if (!String.IsNullOrEmpty(domain))
                {
                    // check for non-existent domain.
                    if (String.Compare(domain, System.Net.Dns.GetHostName(), true) != 0)
                    {
                        if (String.IsNullOrEmpty(LookupDomainSid(domain)))
                        {
                            return null;
                        }
                    }
                    
                    builder.AppendFormat("AND Domain='{0}'", domain);
                }
            }
            
            SelectQuery query = new SelectQuery("Win32_Account", builder.ToString());
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

            try
            {
                foreach (ManagementObject target in searcher.Get())
                {
                    // filter on SID type.
                    byte? sidType =  target["SIDType"] as byte?;

                    if (sidType == null || sidType.Value == 3 || sidType.Value > 5)
                    {
                        continue;
                    }

                    // create account info object.
                    AccountInfo account = new AccountInfo();

                    account.Name = target["Name"] as string;
                    account.SidType = (AccountSidType)sidType.Value;
                    account.Sid = target["SID"] as string;
                    account.Domain = target["Domain"] as string;
                    account.Description = target["Description"] as string;
                    account.Status = target["Status"] as string;
                                        
                    string caption = target["Caption"] as string;
                    object InstallDate = target["InstallDate"];
                    bool? LocalAccount = target["LocalAccount"] as bool?;

                    return account;
                }
            }
            finally
            {
                searcher.Dispose();
            }

            return null;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        public bool ShowDialog(ApplicationAccessRule accessRule)
        {
            AccessTypeCB.SelectedItem = accessRule.RuleType;
            IdentityNameTB.Text       = accessRule.IdentityName;
            m_identity = accessRule.IdentityReference;

            if (m_identity == null)
            {
                AccountInfo account = AccountInfo.Create(IdentityNameTB.Text);

                if (account != null)
                {
                    m_identity = account.GetIdentityReference();
                }
            }

            if (accessRule.Right != ApplicationAccessRight.None)
            {
                AccessRightCB.SelectedItem = accessRule.Right;
            }

            if (ShowDialog() != DialogResult.OK)
            {
                return(false);
            }

            accessRule.RuleType          = (AccessControlType)AccessTypeCB.SelectedItem;
            accessRule.Right             = (ApplicationAccessRight)AccessRightCB.SelectedItem;
            accessRule.IdentityReference = m_identity;

            return(true);
        }
All Usage Examples Of Opc.Ua.Configuration.AccountInfo::Create