【问题标题】:Security context of a new process started from windows service C#从 Windows 服务 C# 启动的新进程的安全上下文
【发布时间】:2012-12-29 20:18:15
【问题描述】:

我有一个在登录名下运行的 Windows 服务,例如“UserA”。 Windows 服务的工作是在计时器经过的事件上启动一个新进程。

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Task.Factory.StartNew(() => StartNewProcess());
}

private void Initialize()
{
    newProcess = new Process();
    newProcess.StartInfo.FileName = "Test.exe";
    newProcess.StartInfo.Arguments = "sessionId...";
    newProcess.StartInfo.CreateNoWindow = false;
    newProcess.StartInfo.ErrorDialog = false;
    newProcess.StartInfo.RedirectStandardError = true;
    newProcess.StartInfo.RedirectStandardInput = true;
    newProcess.StartInfo.UseShellExecute = false;
    newProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;            
}

private void StartNewProcess()
{
    newProcess.Start();
}

在任务管理器中,我看到 Windows 服务和新进程都有一个“用户名”作为“UserA”。

但问题是 Windows 服务能够访问“C:\QueryResult”,但新进程无法访问“C:\QueryResult”

我在这两个过程中都使用File.Copy("C:\QueryResult", "D:\blahblah")

新进程中的安全上下文是否发生了变化?

【问题讨论】:

  • 您是否尝试过将进程的安全上下文记录到文件或事件日志中?
  • 我得到 UnauthorizedAccessException:对路径“C:\QueryResult”的访问被拒绝。
  • Habo - 是的,尝试登录当前线程 Thread.CurrentPrincipal.Identity.Name 有趣的是,在 Windows 服务中,名称是“UserA”,而在新进程中 Thread.CurrentPrincipal.Identity.Name 是 emtpy。

标签: c# windows multithreading service process


【解决方案1】:

尝试像这样自动提升应用程序权限:

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;

public static class Program
{
    [STAThread]
    public static void Main()
    {
        if (!IsAdmin() && IsWindowsVistaOrHigher())
            RestartElevated();
        else
            DoStuff();
    }

    private static Boolean IsAdmin()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();

        if (identity != null)
            return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);

        return false;
    }

    private static Boolean IsWindowsVistaOrHigher()
    {
        OperatingSystem os = Environment.OSVersion;
        return ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 6));
    }

    private static void RestartElevated()
    {
        String[] argumentsArray = Environment.GetCommandLineArgs();
        String argumentsLine = String.Empty;

        for (Int32 i = 1; i < argumentsArray.Length; ++i)
            argumentsLine += "\"" + argumentsArray[i] + "\" ";

        ProcessStartInfo info = new ProcessStartInfo();

        info.Arguments = argumentsLine.TrimEnd();
        info.CreateNoWindow = false;
        info.ErrorDialog = false;
        info.FileName = Application.ExecutablePath;
        info.RedirectStandardError = true;
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;
        info.Verb = "runas";
        info.WindowStyle = ProcessWindowStyle.Hidden; 
        info.WorkingDirectory = Environment.CurrentDirectory;

        try
        {
            Process.Start(info);
        }
        catch { return; }

        Application.Exit();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多