简体   繁体   English

使用Impersonate从C#.NET打开PDF文件

[英]Open a PDF file from C#.NET using Impersonate

I have a folder on the server containing pdf files(Windows Server 2008 R2 Enterprise). 我在服务器上有一个包含pdf文件的文件夹(Windows Server 2008 R2 Enterprise)。 I have to open the folder with the authorized user account and display the pdf file in the browser. 我必须使用授权的用户帐户打开文件夹,并在浏览器中显示pdf文件。 The user has full control permissions on the folder and is a member of the Administrator group. 该用户具有对该文件夹的完全控制权限,并且是Administrator组的成员。

Bellow code works from my local as it opens the pdf file from the folder located in the server in Adobe Reader. 波纹管代码在我的本地计算机上起作用,因为它从Adobe Reader服务器中的文件夹中打开pdf文件。 But on the server the process does not start(Adobe Reader does not open) and no exception occurs. 但是在服务器上,该过程无法启动(Adobe Reader无法打开),并且不会发生异常。 Most of the forums say that turning off UAC will help, but I don't want to do it, because of security reasons. 大多数论坛都说关闭UAC会有所帮助,但出于安全原因,我不想这样做。

How can I deal that issue? 我该如何处理? Please help. 请帮忙。

try
{
    WindowsIdentity wi = new WindowsIdentity(@"user_name@DOMAIN");                
    WindowsImpersonationContext ctx = null;   

    try
    {
        ctx = wi.Impersonate();

        // Thread is now impersonating you can call the backend operations here...
        Process p = new Process();

        p.StartInfo = new ProcessStartInfo()
        {
            CreateNoWindow = true,
            Verb = "open",                        
            FileName = ConfigurationManager.AppSettings["mobil"] + "\\" + prmSicilNo + "_" + prmPeriod.ToString("yyyyMM") + ".pdf",                                            
        };

        p.Start();            
    }
    catch (Exception ex)
    {
        msj = ex.Message;
    }
    finally
    {
        ctx.Undo();
    }                             

    return msj;
}
catch (Exception ex)
{        
    return msj + "Error: " + ex.Message;
}

尝试在服务器上使用以管理员身份运行来执行.exe,如果运行正常,请添加以下代码:

p.StartInfo.Verb = "runas";

@Chintan Udeshi Thank you for you quick answer. @Chintan Udeshi谢谢您的快速回答。 I can run the AcroRd32.exe by Run as Administrator but when I tried with runas I got the error which says; 我可以通过Run as Administrator 身份 Run as Administrator来运行AcroRd32.exe ,但是当我尝试使用runas ,出现了错误消息: "No application is associated with the specified file for this operation" . “没有应用程序与此操作指定的文件关联”

I also tried to place absolute Acrobat Reader Path, but it still didn't work. 我也尝试放置绝对的Acrobat Reader Path,但是它仍然没有用。 Any idea? 任何想法?

p.StartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe")
{
   CreateNoWindow = true,
   Verb = "runas",                        
   FileName = ConfigurationManager.AppSettings["mobil"] + "\\" + prmSicilNo + "_" + prmPeriod.ToString("yyyyMM") + ".pdf", // "c:\\pdf\\",                                            
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM