加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-13 20:38:53 所属栏目:Windows 来源:网络整理
导读:我需要获取一个唯一标识当前Windows用户登录会话的值.这适用于winforms应用程序,而不是ASP.NET.我将从多个进程中检索这个,因此在同一个登录会话中检索时需要返回相同的值.在所有用户会话期间,它只需要在当前机器上是唯一的 – 例如直到机器下次重启. 我认为W
我需要获取一个唯一标识当前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;
        }

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读