The WebClient call is not automatically authenticated. Pass along the authentication cookie of the user with the WebClient request and you should be authentication as that user.
Frederik
Thank you. I tried passing the default credentials and that did not work.
Do you know the C# syntax for passing the authentication cookie? (Sorry, but I am a bit new on this.)
I think you need to use HttpClient or HttpWebRequest instead (both offer support for cookies).
Frederik
Here's also an example how to do it with the HttpClient: http://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage
OK, after a bit of research and a lot of trial and error, plus the help of a good friend, I have figured out how to pull back the content of a file stored in the PageFiles repository and display that in Dynamic Content.
The primary trick to it is that when the Dynamic Control is called, you have to grab the Request.Cookies that are a part of the Page_Load.
using (MyWebClient client = new MyWebClient())
{
var url = string.Format("{0}{1}", Settings.Instance.SiteUrl.ToString(), this.CodeFile.Value.ToString());
HtmlDocument doc = client.GetPage(url, Request.Cookies);
Then, in the GetPage method that is a member of MyWebClient, you have to convert the Request.Cookies HttpCookieCollection to a Net CookieContainer object.
I don't know if this is the most efficient way to make the conversion, but one way that works is:
string[] keys = cookies.AllKeys;
foreach (string key in keys)
{
Cookie newcookie = new Cookie(key, cookies.Get(key).Value);
newcookie.Domain = (!String.IsNullOrEmpty(cookies.Get(key).Domain)) ? cookies.Get(key).Domain : Settings.Instance.SiteUrl.Host;
newcookie.Expires = cookies.Get(key).Expires;
newcookie.HttpOnly = cookies.Get(key).HttpOnly;
newcookie.Path = cookies.Get(key).Path;
newcookie.Secure = cookies.Get(key).Secure;
_cookies.Add(Settings.Instance.SiteUrl, newcookie);
}
request.CookieContainer = _cookies;
Then, the HttpWebResponse that comes back has the document requested, rather than the login screen.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
One other little issue is that the cookies in the HttpCookieCollection each have the domain equal to null. So, you have to set the domain to the Settings.Instance.SiteUrl.Host value.
I have a Dynamic Content plugin that is supposed to allow an author to display the text content of either a page file or a global file. The file location of the file is specified in a PropertyUrl property. Using this property and the SiteUrl property, I construct the full URL and access it using the WebClient.DownloadData method, like so.
var url = string.Format("{0}{1}", Settings.Instance.SiteUrl.ToString(), this.CodeFile.Value.ToString());
var data = client.DownloadData(url);
where the client object is a System.Net.WebClient object. My code generates the correct URL. So, for example, the URL ends up like
http://localhost/PageFiles/10095/codesample.txt
However, the data that is returned from the client.DownloadData call is the EPiServer login page. This seems to indicate that the Dynamic Content call in the editor is not authenticated with the author's credentials.
What has to be done so the plugin uses the author's credentials during the call?