简体   繁体   English

如何强制应用程序不以管理员身份运行

[英]How to force an app to not be run as administrator

是否可以禁用以管理员身份运行的应用程序,只是以本地用户身份运行。

I don't think you can prevent starting the process as administrator; 我不认为你可以阻止以管理员身份启动这个过程; however, you can check if it is executing with elevated privileges, and exit if it's the case. 但是,您可以检查它是否以提升的权限执行,如果是这种情况则退出。

    static bool IsRunningWithElevatedPrivileges()
    {
        IntPtr hToken;
        int sizeofTokenElevationType = Marshal.SizeOf(typeof(int));
        IntPtr pElevationType =
            Marshal.AllocHGlobal(sizeofTokenElevationType);

        if (OpenProcessToken(GetCurrentProcess(), TokenQuery, out hToken))
        {
            uint dwSize;
            if (GetTokenInformation(hToken,
                TokenInformationClass.TokenElevationType, pElevationType,
                (uint)sizeofTokenElevationType, out dwSize))
            {
                TokenElevationType elevationType = (TokenElevationType)Marshal.ReadInt32(pElevationType);
                Marshal.FreeHGlobal(pElevationType);

                switch (elevationType)
                {
                    case TokenElevationType.TokenElevationTypeFull:
                        return true;
                    default:
                        //case TokenElevationType.TokenElevationTypeLimited:
                        //case TokenElevationType.TokenElevationTypeDefault:
                        return false;
                }
            }
        }

        return false;
    }

    [DllImport("kernel32.dll")]
    static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool OpenProcessToken(
        IntPtr processHandle,
        uint desiredAccess,
        out IntPtr tokenHandle);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool GetTokenInformation(
        IntPtr tokenHandle,
        TokenInformationClass tokenInformationClass,
        IntPtr tokenInformation,
        uint tokenInformationLength,
        out uint returnLength);

    const UInt32 TokenQuery = 0x0008;

    enum TokenElevationType
    {
        TokenElevationTypeDefault = 1,
        TokenElevationTypeFull,
        TokenElevationTypeLimited
    }

    enum TokenInformationClass
    {
        TokenUser = 1,
        TokenGroups,
        TokenPrivileges,
        TokenOwner,
        TokenPrimaryGroup,
        TokenDefaultDacl,
        TokenSource,
        TokenType,
        TokenImpersonationLevel,
        TokenStatistics,
        TokenRestrictedSids,
        TokenSessionId,
        TokenGroupsAndPrivileges,
        TokenSessionReference,
        TokenSandBoxInert,
        TokenAuditPolicy,
        TokenOrigin,
        TokenElevationType,
        TokenLinkedToken,
        TokenElevation,
        TokenHasRestrictions,
        TokenAccessInformation,
        TokenVirtualizationAllowed,
        TokenVirtualizationEnabled,
        TokenIntegrityLevel,
        TokenUIAccess,
        TokenMandatoryPolicy,
        TokenLogonSid,
        MaxTokenInfoClass
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows XP上以管理员身份运行其他应用程序 - How to run another app as administrator on Windows XP 如何使用用户名和密码以管理员身份运行app - How to run app as administrator using username and password 如何强制我的项目在Visual Studio 2013中始终以管理员身份运行? - How to force my project in Visual Studio 2013 to always run as Administrator? 如何强制我的 .NET 应用程序以管理员身份运行? - How do I force my .NET application to run as administrator? 如何以管理员身份运行脚本 - How to run script as administrator dotnet 核心应用程序以管理员身份运行 - dotnet core app run as administrator 如何使控制台应用程序始终以管理员身份运行? - How do I make a console app always run as an administrator? 如何强制我的 C# Winforms 程序在任何计算机上以管理员身份运行? - How to force my C# Winforms program run as administrator on any computer? Visual Studio安装项目:如何强制卸载程序以管理员模式运行? - Visual Studio Setup Project: How Can I force an Uninstaller to run in Administrator mode? 如何强制用户在WinForms中的用户管理员帐户 - How to force user to user Administrator account in WinForms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM