■ WindowsIdentity 클래스 : 윈도우즈 로그인 계정 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


Program.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Security.Principal;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.Title = "WindowsIdentity 클래스 : 윈도우즈 로그인 계정 구하기";

 

            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

 

            IEnumerable<string> accountEnumerable = from identityReference in windowsIdentity.Groups

                                                    select new SecurityIdentifier(identityReference.Value)

                                                        .Translate(typeof(NTAccount)).Value;

 

            string microsoftAccount = (from account in accountEnumerable where account.StartsWith(@"MicrosoftAccount\")

                                       select account).FirstOrDefault();

 

            if(microsoftAccount == null)

            {

                Console.WriteLine(windowsIdentity.Name);

            }

            else

            {

                Console.WriteLine(microsoftAccount);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG