C# 运行时提高应用程序权限 [英] C# runtime raising App privileges

查看:58
本文介绍了C# 运行时提高应用程序权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了 Stackoverflow.com 中描述的所有可能的解决方案,但我无法以管理员身份运行应用程序或提示输入管理员权限.

I already tried every possible solution described in Stackoverflow.com, but I can't make an Application run as Administrator or prompt for Administrator privileges.

我试过了:

  • 使用 runAs="requireAdministrator" 创建清单
  • 手动设置verb = 'runAs'"

但是每个实例只是调用另一个没有权限的实例,它只是一直循环.:(

But every instance just calls another that hasn't permissions, it just goes looping all the way. :(

这个项目很简单,我找不到哪里出了问题.

The project is pretty simple and I can't find what is wrong.

你会这么温柔地帮助我吗?!

Would you be so gentle to help me?!

谢谢!!!

循环行为发生在 Windows 7 中.在 Windows XP 中,它要求获得许可,甚至输入有效的用户/密码似乎都失败了.它不会循环,但只会调用没有管理员权限的第一个实例.

The looping behavior happens in Windows 7. With Windows XP it asks for permission, and even typing a valid user/password seems to fail. It won't loop, but calls only the first instance that doesn't have Administrator Privileges.

编辑 2:相同的代码在 Windows 8 中运行良好.Windows XP 没有 UAC,因此无法运行.该问题仅在 Windows 7 中仍然存在.

EDIT 2: The same code works fine in Windows 8. Windows XP hasn't UAC so it won't work. The problem persists with Windows 7 only.

编辑 3:经过多次尝试,我得出结论,如果用户设置为通用用户"配置文件(Windows 中的默认设置),应用程序不会提示管理员访问权限.这似乎很奇怪,是否应该拥有管理员配置文件,以便应用程序可以提示管理访问权限?!:S

EDIT 3: After several tries, I concluded that with a user set with the common "User" profile (default in Windows), the app does not prompt for Administrator access. That seems odd, should one have Administrator profile so the app can prompt for Administration access?! :S

我的清单

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

代码...

public partial class MainWindow : Window
{
    public MainWindow()
    {
        Juca.raisePermissions();
        InitializeComponent();

        if (Juca.gotPower())
        {
            lblBad.Visibility = Visibility.Hidden;
            lblGreat.Visibility = Visibility.Visible;
        }
        else
        {
            lblBad.Visibility = Visibility.Visible;
            lblGreat.Visibility = Visibility.Hidden;
        }
    }
}

class Juca
{
    public static bool gotPower()
    {
        var IdentidadeWindows = WindowsIdentity.GetCurrent();
        var IdentidadePrincipal = new WindowsPrincipal(IdentidadeWindows);
        return IdentidadePrincipal.IsInRole(WindowsBuiltInRole.Administrator);
    }

    public static void raisePermissions()
    {
        if (!gotPower())
        {
            MessageBox.Show("I shall raise your permissions");

            var processInfo = new ProcessStartInfo(Assembly.GetCallingAssembly().CodeBase);
            processInfo.UseShellExecute = true;
            processInfo.Verb = "runas";
            try
            {
                Process.Start(processInfo);
            }
            catch (Win32Exception ex)
            {
                MessageBox.Show("Could not raise at all... :(");
            }
        }
    }
}

推荐答案

您在 ProcessStartInfo 中指定了runas"动词.Runas"显示为以管理员身份运行".它适用于管理员帐户.对于普通用户配置文件,只有runasuser"可用,shell 会打开一个登录对话框,输入管理员帐户名和密码.

You specified "runas" verb in ProcessStartInfo. "Runas" is show as "Run as administrator". It is available for administrator accounts. For common user profile, only "runasuser" available, shell will open a login dialog to input administrator account name and password.

您可以在HKEY_CLASSES_ROOT\exefile\shell 中查看.exe 文件的动词

You can check verbs of .exe file in HKEY_CLASSES_ROOT\exefile\shell

这篇关于C# 运行时提高应用程序权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆