简体   繁体   中英

Open a PDF file from C#.NET using Impersonate

I have a folder on the server containing pdf files(Windows Server 2008 R2 Enterprise). I have to open the folder with the authorized user account and display the pdf file in the browser. The user has full control permissions on the folder and is a member of the Administrator group.

Bellow code works from my local as it opens the pdf file from the folder located in the server in Adobe Reader. But on the server the process does not start(Adobe Reader does not open) and no exception occurs. Most of the forums say that turning off UAC will help, but I don't want to do it, because of security reasons.

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. I can run the AcroRd32.exe by Run as Administrator but when I tried with runas I got the error which says; "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. 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\\",                                            
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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