Supporting OpenId in an asp.net MVC site

18. December 2009

As part of my Stackoverclone project I wanted to stay true the the original site and use OpenID authentication instead of rolling my own. Having never implemented it before, I was pleasantly surprised how easy it was to get a simple example up and running.

I started with the DotNetOpenAuth auth library, and an empty MVC project. Getting the handshake to work is quite simple. They actually have a pretty good page set up that shows you the basics on being a relying party in both webforms and the MVC framework.

First step to create an OpenID logon for ASP.NET mvc is to implement a controller that controls the login flow. A sample of the login methods is provided below.

The LogOn method without any parameters is the action that will be invoked when the logon procedure starts. Because openID also returns to this action after logon we need to check if there is a response from the provider before continuing.

After the user has entered an OpenID url and clicks Logon in the view, the user is directed to the LogOn(String loginIdentifier) method which creates the actual logon request.

using System.Web.Mvc;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;

public ActionResult LogOn()
{
    ViewData["message"] = "You are not logged in";
    var openid = new OpenIdRelyingParty();

    IAuthenticationResponse response = openid.GetResponse();
    if (response != null && response.Status == AuthenticationStatus.Authenticated)
        ViewData["message"] = "Success! Identifier: " + response.ClaimedIdentifier;
     
    return View("Index");
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult LogOn(string openid_identifier)
{
    var openid = new OpenIdRelyingParty();
    IAuthenticationRequest request = openid.CreateRequest(Identifier.Parse(openid_identifier));
    
    return request.RedirectingResponse.AsActionResult();
}

oidSelectorAfter I knew that was working, I went looking for a pre-built control that would give me the same functionality as the one the SO team uses. I found a nice jQuery control, and am quite pleased with it. It won’t take too many modifications to make it look exactly how I want.

All in all, it was only a few short minutes before I went from not having done any OpenID work before, to getting something up an running. To me, that is one of the signs of a well built API. Congrats to the team for creating something so easy to use.

DotNetOpenIdTest.zip (1.95 mb)

asp.net, mvc Framework, stackoverclone , ,

Comments

12/18/2009 3:36:35 PM #
Supporting OpenId in an asp.net MVC site

You've been kicked (a good thing) - Trackback from DotNetKicks.com
12/23/2009 3:07:19 AM #
Social comments and analytics for this post

This post was mentioned on Twitter by jotato: Supporting OpenId in an asp.net MVC site: http://bit.ly/8qx1vH
12/27/2009 7:56:46 PM #
OpenId is more and more common these days, when will live ID be one of the OpenIDs?
12/28/2009 7:07:54 AM #
@Jack They announced that they were going to support it over a year ago, but it has been slow. The last time anything was said was back in August of 2009 http://j.mp/4ZyRbA

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading