微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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;
        }

    }
}

原文地址:https://www.jb51.cc/windows/372009.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐