Dynamic Content Control Needing to Log In to Access Content


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


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?




Nov 11, 2013 20:12

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.


Nov 11, 2013 21:16

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.)

Nov 11, 2013 21:20

I think you need to use HttpClient or HttpWebRequest instead (both offer support for cookies).


Nov 11, 2013 21:36

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

Nov 11, 2013 21:39

Thanks. I will look at that StackOverflow.

Nov 11, 2013 22:58

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();




Nov 12, 2013 17:50

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.

Nov 12, 2013 17:52
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.