Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

July 2008 - Posts

Making authenticated calls to flickr

There are few things to know , when getting photos from your stream , adding comments and overall doing adding and deletion of your photos. As with flickr you can take a look at this url => http://www.flickr.com/services/api/auth.spec.html  for authentication spec to learn more. But with Athena (formerly known as LINQ.Flickr) it's pretty easy to get things going. Last week, I made an update to the FlickrXplorer (The MVC starter project you don't want to miss :-)) project that now enables you to add comments for photos. There is a one click login that will take you to flickr, ask you once for the permission to grant the app for data access and finally will take you back to the place from where you were left off.

image

Now, concept with MVC app is to call a controller url , and finally point flickr to a second controller url that will bring the user to last page, if everything works fine. This whole thing is done by Athena on behalf without a single line of code on client app.

To start off , lets create a context class.

FlickrContext context = new FlickrContext();

Authentication done in two ways, first one is the auto authentication.

var query = from photo in context.Photos
                    where photo.SearchText == "Home" && photo.ViewMode == ViewMode.Owner
                    select photo;

This query tries to search in my photo stream with word "Home",  where the owner is the caller himself. This will automatically take me to the flickr authentication page. However, this is good for desktop based application, where it will fire up the browser , let me authenticate and on closing, it will continue from next line of code. This will also work for web apps, but in case if we need to do something else on successful return, we need a manual way for sure.

context.Authenticate();

Lets say, I call /Photo/Security/Auth url which runs the above line in the controller class that will eventually fire the authentication  process. Here to note that once the authentication process is complete all the successive calls will return Authentication token class, which we can use for showing user status or like "Welcome XYZ...".

AuthToken token = context.Authenticate();

if (token != null)
{
   People people = (from p in context.Peoples
                               where p.NsId == token.UserId
                               select p).Single();
// do the rest.
}

Finally, if we are on a public computer, we surely don't want to leave without logging off. So, there should be a manual call for that as well.

context.ClearToken();

And, it says all. Technically, Athena stores a token in the App Directory for desktop application with the given path in config for FlickrSettings. In web application, it stores the token in user's browser cookie with 30 days of lifeline for current site which of course goes out as soon as you logout. Token is used by flickr for validating authenticated calls and also by the API to make useful decisions in minimizing service calls.

Hope that gives some light on those who likes to give authenticated features to their personal photo app without much of hair loss.

Again, check things out : www.codeplex.com/linqflickr.

kick it on DotNetKicks.com

Posted: Jul 29 2008, 02:16 AM by mehfuzh | with 2 comment(s) |
Filed under: , , ,
Podcast Lifestyle but sometimes with if and elses

 

image
If you listen to podcast, you will find that Internet is full of it. I do enjoy listening how people talk about things in podcast. I generally listen to hanselminutes , dotnetrocks (Carl Franklin) and few obscure ones.  All the episodes of hanselminutes  are available in Itunes. Generally I download the latest episode sync into my ipod touch then as my wife busy cooking, I put in the phones in my ear and try to give out some hand, though I remember only to comment and watch but often help preparing salads :-).  So, last February  before coming from Kuala Lumpur , I bought an IPod touch. Me and my wife both enjoy  listening to it, I watched  all the Mix 08 videos on it, I do use it to update my twitter status when I am at a coffee shop using the WIFI. Finally, browsing Internet with it a real fun,  usually with the tap and zoom feature.

Anyway, its nice to listen to podcasts and talk shows which has become a part of daily lives. Starting from nowhere it is now a source for good knowledge.  As recently, I have come to know a cool xaml tool named Kaxaml and Git source control from Linus Torvalds himself. Also, it is nice hear from dotnetrocks about how Phill Hack get into Microsoft or the great Scott Guthrie wrote MVC framework while flying on an airplane. 

While I have busy time at work through out the day. I do enjoy listening to all these and more , when I need a thoughtful relax and looking out to ideas for implementing or enhancing my own personal projects.

Now coming back to Ipod, its really a cool device starting from browsing while you are at starbucks , listening to music or watching TV shows when you are on the go. But, one thing for sure it has Itunes bundled with it, so beware of your wallet. One good thing about Zune (Yes the only Zune) that you can sync contents wirelessly and sometimes listen to FM radio , that this great device is not able to provide yet. Also, to be honest I didn't like costly money oriented (or object oriented you say) upgrades that apple offers, it really gives me pain. Yes, this is good device but the dark tails with it goes directly to your wallet. It could have been a more popular device if there were some generosity shown by apple for its content management, DRM rules and pricing.  But still with BOSE accessories (sound-dock) , its a fun device you don't want to miss :-).

That's it that came to my mind for now. Also, I want to draw the finish line before the post becomes a soap opera :-) . As it sounds it is a non-technical post , here I haven't talked about LINQ, using LinqExtender toolkit, MVC or anything like it. But do throw me your opinion.

Posted: Jul 09 2008, 04:02 AM by mehfuzh | with 2 comment(s) |
Filed under: , ,
Things to know for hosting your ASP.NET MVC app

When you are planning to host your asp.net MVC application under IIS7 integrated mode environment. There are few easy tweaks that can make your application or starter kit work right out of the box but also can save a lot of your time finding things out.

You might see a Http 404 page if default document is not configured properly. One way of getting this around by using the following in global.asax during the App_BeginRequest

if (Context.Request.FilePath == "/") Context.RewritePath("Default.aspx");

But rater doing code changes, another easy way of achieving it by adding the following snippet in your web.config

<defaultDocument enabled="true">
  <files>
    <clear />
    <add value="Default.aspx" />
  </files>
</defaultDocument>

Relative urls like one that is shown below

<img alt="Flickr Logo" src="/Views/Shared/images/flickr_logo.gif"" />

This will result in invalid output when the app is hosted under IIS7 and if it is hosted under a virtual directory  for example, http://localhost/flickrweb. It will produce url  http://localhost/Views/Shared/images/flickr_logo.gif rather the original http://localhost/ flickrweb/Views/Shared/images/flickr_logo.gif. To overcome this we would need to use Page.ResolveClientUrl  which makes the above snippet look like

<img alt="Flickr Logo"
src="<%= Page.ResolveClientUrl("~/Views/Shared/images/flickr_logo.gif") %>"/>

Now, while hosting our MVC app to a shared hosting server whether or not they provide us a wwwlogs folder, we can easily write down a custom HttpLog module for IIS7 integrated mode (blessing of integrated pipeline). On the Init method of the HttpModule we can add the following event mapping under which we can write our custom logging logic.

context.LogRequest +=new EventHandler(context_LogRequest);

For, each http request this will be fired for IIS7 integrated mode and  we need to add the mapping for it in the following way.

<add name="<ModuleName>" preCondition="integratedMode" 
type="<namespace>, <assembly>"/>

We might be using CSS handlers  that will be compressing the contents and combine cross browser CSS intelligently, like one I have posted few months back

http://weblogs.asp.net/mehfuzh/archive/2008/01/25/keep-your-css-files-clean-with-a-tiny-httphandler.aspx

Make sure you have added the mapping for this as well properly under system.webserver node for integrated mode

<add name="<handlerName>" path="<path>.axd" verb="*"
 type="<namespace>" 
 requireAccess="Script" preCondition="integratedMode" />

And of course add it with Page.ResolveClient url to make it an absolute work.

<link rel="Stylesheet"
 href="<%= Page.ResolveClientUrl("~/CSSHander.axd") %>" type="text/css" />

Now, hosting extension less web app is all beauty but how if someone tries to overload your server by manually hitting /Views/Controller/SomePage.aspx. We can make sure of it that no .aspx file is ever processed by manual hits, by putting an extra web.config file under /Views folder of you ASP.NET MVC application and adding this tiny line inside it

<add name="InvalidAspxHandler" path="*.aspx" verb="*" preCondition="integratedMode" 
type="System.Web.HttpNotFoundHandler, System.Web, Version=2.0.0.0, Culture=neutral
, PublicKeyToken=b03f5f7f11d50a3a"/>

Now, all the config changes I have shown here are to be added under system.webserver node to work with IIS7 integrated mode. The typical webserver node for IIS7 integrated mode looks like

<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <modules runAllManagedModulesForAllRequests="true" />
  <handlers/>
</system.webServer>

Enjoy!!

kick it on DotNetKicks.com

Posted: Jul 06 2008, 04:43 PM by mehfuzh | with 2 comment(s) |
Filed under: , , ,
More Posts