New Social API Web Application Toolkit for .NET web developers

image

What’s New?

I’ve just published an update to our Web Application Toolkits!  If you’re not familiar with Web Application Toolkits (WATs) then here’s a summary:

FREE Web App Toolkits help ASP.NET web developers complete common web development tasks and quickly add new features to your apps. Whether it’s Bing Maps integration or adding social capabilities to your site, there’s a toolkit for you. For the full list of Web Application Toolkits check out this website.

Here is a summary of the work we’ve done in this release:

  1. Added a new Social API Web Application Toolkit
  2. Updated all the WATs to be compatible with Visual Studio 2010
Introducing the Social API Web Application Toolkit
Summary

As social networking Web sites are becoming more and more popular, users often want to access simultaneously the different networks they belong to from one only entry point. For example, one user might want to post the same message they are posting on your site also on Facebook, Twitter, MySpace and so on.

Although many of these social networks provide APIs for accessing their information, you might want to integrate your Web application with several social sites at the same time and be able to do this in a consistent manner, without having to go into numerous modifications in your code with each new social network that you want to incorporate.

This Web Application Toolkit provides a generic “Social Networks” API that allows connecting your Web application with different social networks and managing them through one entry point with a consistent set of methods. In the Toolkit you’ll find examples of how to use the Social Networks API provided to connect a Web application with Facebook and Twitter, allowing you to manage the data provided by these networks in a generic way.

Please notice that this Toolkit includes examples only for reduced set of operations (mainly posting status updates) within the two social networks named before. Through this documentation you’ll find instructions on how to extend this set into more operations and more social networks.

Details

This toolkit comes with Facebook and Twitter Providers that allow you to perform tasks against different Social Network APIs in a common way.  For example, Facebook and Twitter do authentication in different ways which is a pain because you have to write different code for each network.  The Providers mean that you can call common methods and pass in which social networks you want to perform the action against – behind the scenes the providers call the appropriate methods against Facebook or Twitter to get the job done.  The provider model also makes it easy to extend the API to other Social Networks in the future – we’ve provided detailed instructions on how to do this in the documentation that comes with the toolkit download – it’s in the “next steps” section.

  1. public ActionResult NetworkLogin(string providerName)
  2.         {
  3.             var social = new SocialProxy();
  4.             return this.Redirect(social.GetProvider(providerName).LoginUrl);
  5.         }

The code above shows how you use the SocialProxy class, included in the toolkit to get the login URL for the given social network.  In this example we then redirect the user to that URL instead of an MVC view in our application.

The Social Networks API checks if the user is already authenticated in the application and if not it authenticates him by using the FormsAuthentication.SetAuthCookie method. The API maintains a user repository with the account information for each user’s social networks.

  1. public bool Login(string providerName, HttpContextBase httpContext)
  2.         {                        
  3.             var provider = this.GetProvider(providerName);
  4.             var identity = provider.GetLoginIdentity(httpContext);
  5.             
  6.             if (identity == null)
  7.             {
  8.                 return false;
  9.             }
  10.  
  11.             if (!httpContext.User.Identity.IsAuthenticated)
  12.             {
  13.                  var userId = this.usersRepository.FindIdentity(identity) ?? this.usersRepository.CreateUser(identity);
  14.                  FormsAuthentication.SetAuthCookie(userId, false);
  15.             }
  16.             else
  17.             {
  18.                  var userId = this.usersRepository.FindIdentity(identity);
  19.                  if (userId != httpContext.User.Identity.Name)
  20.                  {
  21.                      this.usersRepository.AssociateIdentity(httpContext.User.Identity.Name, identity);
  22.                  }
  23.             }
  24.  
  25.             return true;
  26.         }

Notice that new users do not need to create a separate account when registering on the Web application. The API stores the user’s Facebook and Twitter account information, and creates a unique identifier on the user repository for that user to keep them associated.

The API stores the user internal identifier together with the login information for each of its associated social networks, by using the UsersRepository.AssociateIdentity method.

image

Once connected I can do things against multiple social networks at once.  For example, I can call one method and have it update my Facebook and Twitter status automatically.  Here is one of the overload methods in the SocialProxy class that does just that:

  1. public void UpdateStatus(string status)
  2.         {
  3.             this.UpdateStatus(status, this.ConnectedIdentities.ToArray());
  4.         }

image

This is one of the social network actions we’ve provided in the toolkit but there are bunch that we’ve not covered, I’ve included some other operations from Facebook and Twitter that would be interesting to see – you can see them in the next steps section of the documentation that comes with the toolkit download.

Download the Social Network API Web Application Toolkit
  1. Download the Social Network API Web Application toolkit and let me know what you think – either leave comments on this post or give feedback on the MSDN Code gallery.
  2. Don’t forget to check out the other Web Application Toolkits and start using them in your ASP.NET web applications today!
  3. If you have ideas for future Web Application Toolkits then email me james [at] microsoft.com

Footnote: We’ve also stopped working on the “Web App Toolkit for Making Your Website Social” because the Windows Live team have a new API more information here.  We have also stopped working on the REST Web Application Toolkit because the functionality provided in the toolkit is now part of .NET 4.