[英]Installer Permissions Detection
我需要有關權限/安裝程序的一些信息。
我目前在一個項目中,我們需要為我們的應用程序安裝一個安裝程序。 該程序與Windows XP +兼容,因此與XP,Vista,7和8兼容。我需要做的是檢測當前用戶是否有權安裝我們的應用程序。
我找到了關於此事的幾篇文章,但似乎都沒有給我我想要/需要的明確答案。
至於我們的架構,如下:
我們有一個“單擊一次”應用程序,其中包含C ++應用程序,.NET 2.0“ Windows應用程序”和.NET 4.0“ Windows應用程序”-C ++應用程序非常簡單,它基本上只是檢測他們已安裝的.NET版本並委托給該Windows應用程序。 這些Windows應用程序中的每個應用程序本質上都是相同的-它們基本上會進行連接速度檢查,如果通過了連接速度檢查,則會下載並運行適用於我們軟件的MSI安裝程序。
用戶顯然需要具有權限才能安裝我們的應用程序,而我們需要在該鏈的某處(在Windows應用程序中的速度檢查或我不確定的MSI安裝程序的一部分周圍)添加檢測功能。這就是我需要人們幫助的地方。
最好的方法是什么?如何做?
據我所知,它們將圍繞UAC帶來一些復雜性(是否打開,關閉以及它們是本地管理員,域管理員還是普通用戶,還包括他們是否是域用戶並且當前不在網絡上)。 它們還會帶來其他一些復雜性,因為我們還需要補償沒有UAC的XP(實際上我完全不確定如何在XP中檢測到它)。
從我在網上看到的內容來看,有一些選項可以在代碼中進行如下操作: 在.NET / C#中,測試進程是否具有管理特權
清單上還有其他一些選項,例如: 如何強制我的.NET應用程序以管理員身份運行? -清單類型方法可以在XP上使用嗎?
我在此流程中有一些選擇,以及在何處添加此內容,因此我從社區中尋找的內容是有關在哪里/如何滿足我的所有需求的信息。
任何人都可以提供的任何幫助將不勝感激。
謝謝邁克爾
不確定最佳做法,但是如果您想通過用戶位於管理員組中的帳戶來檢測用戶是否具有管理員權限,或者他們是否能夠通過UAC升級為該角色,請查看MSDN代碼示例自我升級: http : //code.msdn.microsoft.com/windowsdesktop/CSUACSelfElevation-644673d3/sourcecode? fileId=21729&pathId= 1041468146
該示例具有MainForm.IsUserInAdminGroup()方法,該方法查看當前用戶的令牌或他們的升級令牌:
/// <summary>
/// The function checks whether the primary access token of the process belongs
/// to user account that is a member of the local Administrators group, even if
/// it currently is not elevated.
/// </summary>
/// <returns>
/// Returns true if the primary access token of the process belongs to user
/// account that is a member of the local Administrators group. Returns false
/// if the token does not.
/// </returns>
/// <exception cref="System.ComponentModel.Win32Exception">
/// When any native Windows API call fails, the function throws a Win32Exception
/// with the last error code.
/// </exception>
internal bool IsUserInAdminGroup()
{
bool fInAdminGroup = false;
SafeTokenHandle hToken = null;
SafeTokenHandle hTokenToCheck = null;
IntPtr pElevationType = IntPtr.Zero;
IntPtr pLinkedToken = IntPtr.Zero;
int cbSize = 0;
try
{
// Open the access token of the current process for query and duplicate.
if (!NativeMethods.OpenProcessToken(Process.GetCurrentProcess().Handle,
NativeMethods.TOKEN_QUERY | NativeMethods.TOKEN_DUPLICATE, out hToken))
{
throw new Win32Exception();
}
// Determine whether system is running Windows Vista or later operating
// systems (major version >= 6) because they support linked tokens, but
// previous versions (major version < 6) do not.
if (Environment.OSVersion.Version.Major >= 6)
{
// Running Windows Vista or later (major version >= 6).
// Determine token type: limited, elevated, or default.
// Allocate a buffer for the elevation type information.
cbSize = sizeof(TOKEN_ELEVATION_TYPE);
pElevationType = Marshal.AllocHGlobal(cbSize);
if (pElevationType == IntPtr.Zero)
{
throw new Win32Exception();
}
// Retrieve token elevation type information.
if (!NativeMethods.GetTokenInformation(hToken,
TOKEN_INFORMATION_CLASS.TokenElevationType, pElevationType,
cbSize, out cbSize))
{
throw new Win32Exception();
}
// Marshal the TOKEN_ELEVATION_TYPE enum from native to .NET.
TOKEN_ELEVATION_TYPE elevType = (TOKEN_ELEVATION_TYPE)
Marshal.ReadInt32(pElevationType);
// If limited, get the linked elevated token for further check.
if (elevType == TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited)
{
// Allocate a buffer for the linked token.
cbSize = IntPtr.Size;
pLinkedToken = Marshal.AllocHGlobal(cbSize);
if (pLinkedToken == IntPtr.Zero)
{
throw new Win32Exception();
}
// Get the linked token.
if (!NativeMethods.GetTokenInformation(hToken,
TOKEN_INFORMATION_CLASS.TokenLinkedToken, pLinkedToken,
cbSize, out cbSize))
{
throw new Win32Exception();
}
// Marshal the linked token value from native to .NET.
IntPtr hLinkedToken = Marshal.ReadIntPtr(pLinkedToken);
hTokenToCheck = new SafeTokenHandle(hLinkedToken);
}
}
// CheckTokenMembership requires an impersonation token. If we just got
// a linked token, it already is an impersonation token. If we did not
// get a linked token, duplicate the original into an impersonation
// token for CheckTokenMembership.
if (hTokenToCheck == null)
{
if (!NativeMethods.DuplicateToken(hToken,
SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
out hTokenToCheck))
{
throw new Win32Exception();
}
}
// Check if the token to be checked contains admin SID.
WindowsIdentity id = new WindowsIdentity(hTokenToCheck.DangerousGetHandle());
WindowsPrincipal principal = new WindowsPrincipal(id);
fInAdminGroup = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
finally
{
// Centralized cleanup for all allocated resources.
if (hToken != null)
{
hToken.Close();
hToken = null;
}
if (hTokenToCheck != null)
{
hTokenToCheck.Close();
hTokenToCheck = null;
}
if (pElevationType != IntPtr.Zero)
{
Marshal.FreeHGlobal(pElevationType);
pElevationType = IntPtr.Zero;
}
if (pLinkedToken != IntPtr.Zero)
{
Marshal.FreeHGlobal(pLinkedToken);
pLinkedToken = IntPtr.Zero;
}
}
return fInAdminGroup;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.