.net讀取Windows登入使用者資訊(downmoon)
如何讀取windows 當前登入使用者的狀態資訊。
主要程式碼分享如下:
一:匯入dll
Code
/**//************************************************************************/
/**//* The following Interop code should be placed in a sealed internal NativeMethod class
* but has been left here to simplify the example.
/************************************************************************/
[DllImport("secur32.dll", SetLastError = false)]
private static extern uint LsaFreeReturnBuffer(IntPtr buffer);
[DllImport("Secur32.dll", SetLastError = false)]
private static extern uint LsaEnumerateLogonSessions(out UInt64 LogonSessionCount, out IntPtr LogonSessionList);
[DllImport("Secur32.dll", SetLastError = false)]
private static extern uint LsaGetLogonSessionData(IntPtr luid, out IntPtr ppLogonSessionData);
[StructLayout(LayoutKind.Sequential)]
private struct LSA_UNICODE_STRING
{
public UInt16 Length;
public UInt16 MaximumLength;
public IntPtr buffer;
}
[StructLayout(LayoutKind.Sequential)]
private struct LUID
{
public UInt32 LowPart;
public UInt32 HighPart;
}
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_LOGON_SESSION_DATA
{
public UInt32 Size;
public LUID LoginID;
public LSA_UNICODE_STRING Username;
public LSA_UNICODE_STRING LoginDomain;
public LSA_UNICODE_STRING AuthenticationPackage;
public UInt32 LogonType;
public UInt32 Session;
public IntPtr PSiD;
public UInt64 LoginTime;
public LSA_UNICODE_STRING LogonServer;
public LSA_UNICODE_STRING DnsDomainName;
public LSA_UNICODE_STRING Upn;
}
private enum SECURITY_LOGON_TYPE : uint
{
Interactive = 2, //The security principal is logging on interactively.
Network, //The security principal is logging using a network.
Batch, //The logon is for a batch process.
Service, //The logon is for a service account.
Proxy, //Not supported.
Unlock, //The logon is an attempt to unlock a workstation.
NetworkCleartext, //The logon is a network logon with cleartext credentials.
NewCredentials, // Allows the caller to clone its current token and specify new credentials for outbound connections. The new logon session has the same local identity but uses different credentials for other network connections.
RemoteInteractive, // A terminal server session that is both remote and interactive.
CachedInteractive, // Attempt to use the cached credentials without going out across the network.
CachedRemoteInteractive, // Same as RemoteInteractive, except used internally for auditing purposes.
CachedUnlock // The logon is an attempt to unlock a workstation.
}
二、呼叫方法,寫入一個ListBox中
Code
public void PopulateListbox()
{
System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
DateTime systime = new DateTime(1601, 1, 1, 0, 0, 0, 0); //win32 systemdate
UInt64 count;
IntPtr luidPtr = IntPtr.Zero;
LsaEnumerateLogonSessions(out count, out luidPtr); //gets an array of pointers to LUIDs
IntPtr iter = luidPtr; //set the pointer to the start of the array
for (ulong i = 0; i < count; i++) //for each pointer in the array
{
IntPtr sessionData;
LsaGetLogonSessionData(iter, out sessionData);
SECURITY_LOGON_SESSION_DATA data = (SECURITY_LOGON_SESSION_DATA)Marshal.PtrToStructure(sessionData, typeof(SECURITY_LOGON_SESSION_DATA));
//if we have a valid logon
if (data.PSiD != IntPtr.Zero)
{
//get the security identifier for further use
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(data.PSiD);
//extract some useful information from the session data struct
string username = Marshal.PtrToStringUni(data.Username.buffer).Trim(); //get the account username
string domain = Marshal.PtrToStringUni(data.LoginDomain.buffer).Trim(); //domain for this account
string authpackage = Marshal.PtrToStringUni(data.AuthenticationPackage.buffer).Trim(); //authentication package
SECURITY_LOGON_TYPE secType = (SECURITY_LOGON_TYPE)data.LogonType;
DateTime time = systime.AddTicks((long)data.LoginTime); //get the datetime the session was logged in
listBox1.Items.Add("User: " + username + " *** Domain: " + domain + " *** Login Type: (" + data.LogonType + ") " + secType.ToString() +" *** Login Time: "+time.ToLocalTime().ToString());
}
iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LUID))); //move the pointer forward
LsaFreeReturnBuffer(sessionData); //free the SECURITY_LOGON_SESSION_DATA memory in the struct
}
LsaFreeReturnBuffer(luidPtr); //free the array of LUIDs
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526511/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- asp.net讀取Windows域(AD)資訊ASP.NETWindows
- Asp.Net MVC記住使用者登入資訊下次直接登入功能ASP.NETMVC
- 根據微信code獲取換取使用者登入態資訊
- 微信小程式授權登入獲取使用者資訊微信小程式
- 微信小程式維護登入態與獲取使用者資訊微信小程式
- 查詢使用者登入資訊sqlSQL
- redis儲存使用者登入資訊Redis
- 記錄ORACLE使用者登入資訊Oracle
- ThinkPHP5-微信小程式獲取使用者授權登入資訊PHP微信小程式
- Spring Security - 獲取當前登入使用者的詳細資訊Spring
- Windows檢視登入使用者會話Windows會話
- App 第三方登入獲取使用者資訊 支付寶登入後端程式碼參考APP後端
- 微信開發筆記——微信網頁登入授權,獲取使用者資訊筆記網頁
- Windows mobile 下讀取手機SIM卡資訊Windows
- .NET Core如何全域性獲取使用者資訊?
- 企業微信-自建H5應用授權登入獲取使用者資訊H5
- 設定Telnet root使用者的登入
- 獲取 CPU資訊,並透過登錄檔寫入
- 原生 PHP 實現支付寶 App 第三方登入獲取 使用者資訊PHPAPP
- C4C和CRM裡獲取當前登入使用者分配的Organization Unit資訊
- 獲取所有域使用者的登陸歷史資訊指令碼指令碼
- Asp.net Core 和類庫讀取配置檔案資訊ASP.NET
- PHP artisan cache:clear 避免把使用者登入資訊也清空PHP
- j_security_check使用者的登入資訊儲存在哪?
- Asp.net中防止使用者多次登入的方法ASP.NET
- Windows遠端桌面設定多使用者同時登入Windows
- 登入介面:從資料庫中獲取資訊驗證登入(與註冊介面相聯絡)資料庫
- 如何在ASP.NET中使用驗證通過的Windows Live ID使用者登入網站ASP.NETWindows網站
- windows自動登入telnet批處理檔案VBS法(Windows Script Host)Windows
- dotnet OpenXML 讀取 PPT 內嵌 ole 格式 Excel 表格的資訊XMLExcel
- Linux 下檢視系統當前登入使用者資訊Linux
- 如何在Linux 讓Telnet root使用者可以登入Linux
- Asp.net2.0提供程式模型--單使用者登入元件ASP.NET模型元件
- 使用Hash直接登入WindowsWindows
- 爬蟲實戰(二):Selenium 模擬登入並爬取資訊爬蟲
- Sniffer pro抓包盜取Telnet登入密碼實驗密碼
- 使用者系列之二:域環境下使用者登入之快取故障快取
- 清理SQL多餘登入資訊SQL