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();
}
After 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)
short link to this post:
asp.net, mvc Framework, stackoverclone
programming, webdev, openid