How to integrate sharepoint document into web application and adding credentials for external users
Being new to integrating sharepoint documents into web applications I am struggling with the following task, so any help or guidance would be very much appreciated.
I have a page in my web application that needs to display a document from sharepoint.
I use the appropiate sharepoint web service to get a list of all the documents in the sharepoint list, I then pick out the one I would like to retrieve and get the path to the document so i would end up with something like:
Company%20Hire/Hire%20Site%20Price%20Lists/0.pdf
开发者_开发问答Within our network I can do following:
iframePdf.Attributes[
"src"] = ConfigurationManager.AppSettings["SharepointUrl"] + _filePath;
This would display the document in iframe... problem I have is coming from outside the network the sharepoint site can't be accessed (due to firewalls etc).
We do have a vpn back to our network on the external server our web application sits on. If i was to use the document url in a browser on the server I am prompted for credentials and then get to view the document.
Is there a way for the web application to use the vpn, use credentials i give (e.g through impersonation) to access the document and display it to exernal clients?
You could consider implementing a handler on your web application that is responsible for downloading documents from SharePoint and streaming them across the network.
One of the more-convenient classes for downloading content is System.Net.WebRequest. This tutorial can help you get rolling with a custom HTTP handler. On your web pages, you would provide to your user hyperlinks to the HTTP handler with, say, query string parameters designating the file to download. Your handler would download the file, set the appropriate content types and content disposition on the response, and copy the web request response bits to the response stream.
The Credentials
property of the WebRequest is important for authenticating your client to SharePoint. It's just a little tricky if you are using NTLM Integrated Windows authentication, due to the double-hop problem. You can work around the double hop a number of ways, including explicitly prompting for user credentials (via a form), implementing Kerberos, or handling authorization yourself while impersonating a fixed privileged user.
Good luck!
I resolved it by doing the following:
I used the web client class, which allows you to add credentials and I just called the download file method. Then got another page to read the pdf from saved location (location saved in a session variable) and write file to Response stream.
Following code did it:
using (var _webclient = new WebClient())
{
_webclient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["DomainUsername"],ConfigurationManager.AppSettings["DomainPassword"],
"domain");
_webclient.DownloadFile(ConfigurationManager.AppSettings["SharePointPortal"] + _filePath, _path);
}
Portal.Common.Objects.CommonSessionHelper.Instance.IframeUrl = _path;
iframePdf.Attributes["src"] = "PdfEmbedder.aspx";
Pdfembeder.aspx.cs is as:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrEmpty(Portal.Common.Objects.CommonSessionHelper.Instance.IframeUrl))
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.WriteFile(Portal.Common.Objects.CommonSessionHelper.Instance.IframeUrl);
Response.Flush();
Response.Close();
Portal.Common.Objects.CommonSessionHelper.Instance.IframeUrl = null;
}
}
catch (Exception error)
{
Response.Write(error.Message);
}
}
精彩评论