如何在Windows中为当前用户的登录会话获取唯一ID – c#

我需要获取一个唯一标识当前Windows用户登录会话的值.这适用于winforms应用程序,而不是ASP.NET.我将从多个进程中检索这个,因此在同一个登录会话中检索时需要返回相同的值.在所有用户会话期间,它只需要在当前机器上是唯一的 – 例如直到机器下次重启.

我认为Windows Logon Id是正确的,但看起来有点痛苦.有没有其他或更简单的方法来获得这个?

我将使用ID包含在命名管道服务的地址中,以在计算机上运行的两个进程之间进行通信.我希望包含登录ID以避免在有多个用户登录时发生冲突,包括同一用户的多个会话.

据我所知,你需要的是:

SID:S-1-5-5-X-Y
名称登录会话
描述:登录会话.这些SID的X和Y值对于每个会话是不同的.

Windows操作系统中众所周知的安全标识符
http://support.microsoft.com/kb/243330

有人在这里要求类似的东西:

How to get the logon SID in C#

他们在那里有一个很好的答案,但我想添加自己的答案

这是我的解决方案:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace TestlogonSid
{
    public partial class Form1 : Form
    {

        private delegate bool EnumDesktopProc(string lpszDesktop,IntPtr lParam);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender,EventArgs e)
        {

            this.textBox1.Text = GetlogonSid.getlogonSid();
        }


    }

    public class GetlogonSid
    {
        //The SID structure that identifies the user that is currently associated with the specified object. 
        //If no user is associated with the object,the value returned in the buffer pointed to by lpnLengthNeeded is zero. 
        //Note that SID is a variable length structure. 
        //You will usually make a call to GetUserObjectinformation to determine the length of the SID before retrieving its value.
        private const int UOI_USER_SID = 4;

        //GetUserObjectinformation function
        //Retrieves information about the specified window station or desktop object.
        [DllImport("user32.dll")]
        static extern bool GetUserObjectinformation(IntPtr hObj,int nIndex,[MarshalAs(UnmanagedType.LPArray)] byte[] pvInfo,int nLength,out uint lpnLengthNeeded);


        //GetThreadDesktop function
        //Retrieves a handle to the desktop assigned to the specified thread.
        [DllImport("user32.dll")]
        private static extern IntPtr GetThreadDesktop(int dwThreadId);


        //GetCurrentThreadId function
        //Retrieves the thread identifier of the calling thread.
        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        //ConvertSidToStringSid function
        //The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display,storage,or transmission.
        //To convert the string-format SID back to a valid,functional SID,call the ConvertStringSidToSid function.

        [DllImport("advapi32",CharSet = CharSet.Auto,SetLastError = true)]
        static extern bool ConvertSidToStringSid(
            [MarshalAs(UnmanagedType.LPArray)] byte[] pSID,out IntPtr ptrSid);


        /// <summary>
        /// The getlogonSid function returns the logon Session string
        /// </summary>
        /// <returns></returns>
        public static string getlogonSid()
        {
            string sidString = "";
            IntPtr hdesk = GetThreadDesktop(GetCurrentThreadId());
            byte[] buf = new byte[100];
            uint lengthNeeded;
            GetUserObjectinformation(hdesk,UOI_USER_SID,buf,100,out lengthNeeded);
            IntPtr ptrSid;
            if (!ConvertSidToStringSid(buf,out ptrSid))
                throw new System.ComponentModel.Win32Exception();
            try
            {
                sidString = Marshal.PtrToStringAuto(ptrSid);
            }
            catch
            {
            }
            return sidString;
        }

    }
}

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...