当前位置: 首页>>代码示例>>C#>>正文


C# NTAccount.ToString方法代码示例

本文整理汇总了C#中System.Security.Principal.NTAccount.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# NTAccount.ToString方法的具体用法?C# NTAccount.ToString怎么用?C# NTAccount.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Principal.NTAccount的用法示例。


在下文中一共展示了NTAccount.ToString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            // domainSid: WindowsIdentity.GetCurrent().User.AccountDomainSid);
            if (args.Length > 0)
            {
                if (args[0].StartsWith("-?") ||
                    args[0].StartsWith("-h") ||
                    args[0].StartsWith("-help") ||
                    args[0].StartsWith("/?") ||
                    args[0].StartsWith("/h") ||
                    args[0].StartsWith("/help"))
                {
                    ShowHelp();
                }
                else if (Enum.IsDefined(typeof(WellKnownSidType), args[0]))
                {
                    try
                    {
                        WellKnownSidType sidType = (WellKnownSidType)Enum.Parse(typeof(WellKnownSidType), args[0], false);

                        SecurityIdentifier sid = null;
                        if (args[0].StartsWith("Account"))
                        {
                            sid = new SecurityIdentifier(sidType, WindowsIdentity.GetCurrent().User.AccountDomainSid);
                        }
                        else
                        {
                            sid = new SecurityIdentifier(sidType, null);
                        }

                        NTAccount NTUser = (NTAccount)sid.Translate(typeof(System.Security.Principal.NTAccount));
                        Console.WriteLine("[" + sidType.ToString() + "]");
                        Console.WriteLine("Name=" + NTUser.ToString());
                        Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\")+1));
                        Console.WriteLine("SID=" + sid.ToString());
                        Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper());

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    if (args[0].StartsWith("S-"))
                    {
                        try
                        {
                            SecurityIdentifier sid = new SecurityIdentifier(args[0]);
                            NTAccount NTUser = (NTAccount)sid.Translate(typeof(System.Security.Principal.NTAccount));

                            Console.WriteLine("[" + sid.ToString() + "]");
                            Console.WriteLine("Name=" + NTUser.ToString());
                            Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\") + 1));
                            Console.WriteLine("SID=" + sid.ToString());
                            Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper());
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    else
                    {
                        try
                        {
                            NTAccount NTUser = new NTAccount(args[0]);
                            SecurityIdentifier sid = (SecurityIdentifier)NTUser.Translate(typeof(SecurityIdentifier));

                            Console.WriteLine("[" + NTUser.ToString() + "]");
                            Console.WriteLine("Name=" + NTUser.ToString());
                            Console.WriteLine("Shortname=" + NTUser.ToString().Substring(NTUser.ToString().IndexOf("\\") + 1));
                            Console.WriteLine("SID=" + sid.ToString());
                            Console.WriteLine("IsAccountSid=" + sid.IsAccountSid().ToString().ToUpper());
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            else
            {
                ShowHelp();
            }
        }
开发者ID:andif888,项目名称:SidToUser,代码行数:88,代码来源:Program.cs

示例2: GrantRegistryAccess

        private static void GrantRegistryAccess(string key, Prison prison)
        {
            NTAccount account = new NTAccount(null, prison.User.Username);
            var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

            using (RegistryKey rk = hklm.OpenSubKey(key, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
            {
                RegistrySecurity rs = rk.GetAccessControl();
                RegistryAccessRule rar = new RegistryAccessRule(
                    account.ToString(),
                    RegistryRights.FullControl,
                    InheritanceFlags.ContainerInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow);

                rs.AddAccessRule(rar);
                rk.SetAccessControl(rs);
            }
        }
开发者ID:solakian,项目名称:windows-isolation,代码行数:19,代码来源:MsSqlInstanceTool.cs


注:本文中的System.Security.Principal.NTAccount.ToString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。