TransmitFile security issue in IE and word - c#

I'm trying to transfer a file (word document) from my server to the client
I'm using this :
Response.TransmitFile(path);
Response.End();
In IE when I click open of the download popup, everything is ok and word open with my document.
The problem is when I click the save button in word, it pop me a security/connection popup. I have to give a username/password for mysite.com

That is normal behaviour since Word handles files opened via browser differently - it tries to verify whether they are "WebDAV-accessible" and uses some MS-specific extensions in doing so... that in turn doesn't use your current browser session but tries/needs to establish a new which in turn leads to Word askting you for credentials.
IF you want to just download it locally you could by using a content-disposition header - this way Word sees it as a local file and tries to save it locally upon "Word save button press".
IF you really want it to be saved back to your site it gets a bit tricky... although as you are running on IIS you might be able to implement something in combination with the IIS-built-in WebDAV functionality...
EDIT - as per comments:
For how to use content-disposition http header see for example http://www.jtricks.com/bits/content_disposition.html

Related

C# Running code on open webbrowser, downloading specific files

So this is a bit of a tricky problem I'm having and I'm starting to think that it's not easily possible.
There's a website that has a level of security that I am trying to automatically download files from(it's a banking site). The website will not save user info when you log in, and after logging in you have to verify a certificate in your browser to receive your reports. Once you've verified the browser with the certificate and are logged in, however, you are free to open up the reports(pdf files) by directly typing in the link to them.
For other sites, I would do something like:
using (WebClient client = new WebClient())
{
client.DownloadFile(PDFFileReportWebsite,#LocalLocationToStoreFile);
}
But for this site, I have no idea what to do(the above doesn't work). Is there a way to have code like above run from within the browser itself after I've logged in and verified my certificate?
One solution I thought of that would be overkill(nevermind just gross/hacky), and I can't imagine the best option, would be to make a webbrowser app in C# and run the code on that browser after logging in.
EDIT:
ProcessStartInfo pi = new ProcessStartInfo(WebAddressOfPdfFileINeed);
Process.Start(pi);
correctly opens up the webbrowser(chrome since its my default) to the file I need(If I'm already logged in and verified my cert). Is there a way to force chrome to save this file or print it as text or a pdf?

Open IManDocument in browser

I am using the WorkSite COM API to open an IMANEXT2Lib.IManFileOpenDlg which is working perfectly. Clicking on the document, I can retrieve the metadata, such as the document name, version, number etc.
Is there a method or technique available to read the document from WorkSite and prompt a download/open through a browser?
The IManage.IManDocument has the method GetCopy() available which will download a local copy to a specified directory, but this isn't really what I'm after.
If the clients have Worksite 8.5 or greater, you can use the IWL protocol in a hyperlink to force the client to use the IWL handler to download and launch the file themselves instead of streaming it from the browser. This has the added benefit of respecting the document's permissions and checking the document out to the user so they can make amends to the file if required.
An IWL formatted anchor tag in a web browser would look like the following:
Document x
Note that this will only work if the clients have Worksite 8.5 or newer installed.

Change Browser download folder using C#

Is there a way I could change the download folder of the default web browser using c#.
Concurring with other's comments, you can only do it in a desktop app if you have the right permissions.
Here's some sample code to find out the default browser of the system (from this post):
private string getDefaultBrowser()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe")+4);
}
}
finally
{
if (key != null) key.Close();
}
return browser;
}
However, things get tricky from here. Different browsers have different ways of saving the default location.
E.g.,
IE may store it in registry (usually under HKEY_CURRENT_USER\ Software\ Microsoft\ Internet Explorer)
FF stores it in prefs.js in Profile folder (checkout this post to get to it via code)
Not sure about Chrome and Safari
but you get the idea.
Not sure what your end goal is, but from a UX standpoint, I think the best thing to do would be to ask user to specify the Download directory (in other words, you expose a Setting in your App for the default download location).
To expand on Ash's comment - if you're within a web app, no. If you're a desktop app, and you have sufficient permissions (i.e. running as Administrator), probably. But you'd need to find the default browser (from the registry presumably) and know how to set the download folder for each popular browser, or every browser you want to work with.
Where are you trying to do this from? If you mean "someone hits our website and ...", the answer is no, as anything you run is in a security context. You can certainly suggest the user changes the folder, but you are stuck.
Assuming you are not a web application, you have options. The main user download directory is located at X under the key {374DE290-123F-4565-9164-39C4925E467B}. Yeah, that sounds like a lot of fun. You can learn how to hack the registry programatically here. But, the user can specify a specific folder in the browser, as well. This means you have to know what browser the user is using and hack it, or you can attempt to hack all.
The bad news is the app, running (most likely) in the user context, may not have administrator rights and be able to whack the registry keys to change the folder.

Save a PDF that's been streamed to the browser

I'm streaming PDFs to the browser, and it works fine. The problem is when someone clicks the save button. On the same server, we have a "Live" and a "test" site. They are identical, and I've verified that all code is identical. However I'm seeing different dialog boxes when the user clicks "Save"
Test site:
- dialog box: "Save A Copy"
- file name: "Document.pdf"
- file type: Adobe PDF
Live site:
- dialog box: "Save As..."
- file name: "Document.aspx"
- file type: Adobe PDF
To make things even more "fun" I've noticed this behavior ONLY with IE8. With Firefox or Chrome clicking save - regardless of which site (test or live) opens the "Save a Copy" dialog box which allows the users to save the file as a PDF instead of a .aspx file.
Any suggestions are appreciated
It might have to do with your IIS mime settings being different between the two servers.
Make sure your HTTP response headers are set properly as well. You'll be able to specify the filename using the content-disposition(?) attachment: filename.pdf (IIRC)

Is there any way to make a PDF savable from inside the browser?

I have a file that I can get to with the UNC \mypath\myfile. I retrieve the path with c# and assign it to a link. The browser opens the file inside itself (ie display in the browser) without problem. But, when I try and save to \mypath\myfile I am prompted to save it locally. If I view the file outside of the browser (IE 7) I can edit and save as expected, again, via the UNC.
What I trying to do is use iframe to display the file from my UNC (file:///\mypath\myfile), which does work, but now I can't edit it. Outside the browser I can.
Is there anyway to save a PDF when displaying it inside the browser? I also tried a button to use the save method on the pdf, but it did not work.
Thank you.
I am using IE 7 and Adobe Professional 7.1.0.
When you open a file via a web browser the web browser downloads the file locally, then sends a local file path to the application that opens it. Even in the case you are using a UNC in your href Adobe does not get that UNC path to save back to, it is getting a local machine path. Keep in mind the browser does the same thing even if the UNC is a local machine path.
I don't think that the behavior you want is possible.
<body>
<div style="height:80px">
<p>Save
</div>
<div>
<iframe width="100%" height="100%" src="pdfname.pdf" />
</div>
</body>
On downloadpdf.aspx's page load event,add:
Response.AddHeader("content-disposition","attachment; filename=pdfname.pdf")
Response.ContentType = "application/pdf"
Server.Transfer("pdfname.pdf")
Untested, but should give you an idea...
Saving a PDF in your browser (through File-Save As in IE etc) or Save A Copy in the Adobe system will always prompt you to save it locally. You could navigate manually to your UNC path, but this is browser behaviour, not server behaviour.

Categories