ChrisP
ChrisP

Reputation: 10116

How to retrieve Google Blogger feed in ASP.NET medium trust?

I have an ASP.NET web site hosted at HostMySite.com and they recently changed the shared accounts to run in medium trust. In my web site I query my Blogger account and get blog posts to display on my web site.

I am using Google.GData.Client v1.4.0.2

The retrieval works locally (and worked until medium trust was invoked at the ISP). Now I receive the following error:

[SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessPermission.Demand() +58
   System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint) +147
   System.Net.HttpRequestCreator.Create(Uri Uri) +26
   System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) +216
   System.Net.WebRequest.Create(Uri requestUri) +31
   Google.GData.Client.GDataRequest.EnsureWebRequest() +77
   Google.GData.Client.GDataRequest.Execute() +42
   Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength) +193
   Google.GData.Client.Service.Query(FeedQuery feedQuery) +202

I've search the Google documentation and on-line but have not been able to find out what I need to change.

Upvotes: 2

Views: 1015

Answers (4)

rtpHarry
rtpHarry

Reputation: 13125

Depending on what you want to use this for you could look at the Google AJAX Feed API which is a javascript api that will let you embed rss feeds into your site.

You wouldn't be able to access this from the server side tho, it would only be display in the users browser.

If you are trying to get this content indexed by search engines, need to save it to a database or manipulate it some how then this solution will not work for you.

Google Feed API Docs are at:

Upvotes: 0

bakes
bakes

Reputation: 11

Some hosts will set the default trust level to "medium" (or a custom trust level that is similar to medium), but allow it to be overridden in your web.config like so:

<system.web>
    <trust level="Full" originUrl="" />
</system.web>

Have you tried to override it?

Upvotes: 1

kamranicus
kamranicus

Reputation: 4387

Are you just retrieving blog posts? Can't an RSS feed do the job for you? This works on a GoDaddy site I did for a client.

Remember to import the namespace (might need to add reference to project):

using System.ServiceModel.Syndication;

protected void Page_Load(object sender, EventArgs e)
    {

        XmlReader xmlReader = System.Xml.XmlReader.Create("URL to blog feed");

        SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
        lstLatestNews.DataSource = feed.Items.Take(5);
        lstLatestNews.DataBind();
    }

In the ASPX page:

<asp:ListView ID="lstLatestNews" runat="server">
        <LayoutTemplate>
            <ul id="latest_news">
                <li id="itemPlaceholder" runat="server"></li>
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li><a href="<%# Eval("Links[0].Uri.AbsoluteUri") %>"><%# Eval("Title.Text") %></a> <em><%# Convert.ToDateTime(Eval("PublishDate.DateTime")).ToString("g") %></em></li>
        </ItemTemplate>
    </asp:ListView>

Upvotes: 0

Justin Grant
Justin Grant

Reputation: 46683

Under medium trust, web apps cannot open remote HTTP connections. There's no workaround that I'm aware of. Your best bet is either to switch to another hoster, or to negotiate with your hoster to do what GoDaddy does where they've relaxed their WebPermission settings specifically to allow sccenarios like yours where server apps must fetch data from remote HTTP servers like Google's.

BTW, I'm not saying that GoDaddy is a great hoster-- they're not-- but they did revise their medium trust settings to address the problem noted in your question. And if a low-end hoster like GoDaddy is willing to do this, then you may be able to use this as an argument to convince other hosters to do the same.

Upvotes: 0

Related Questions