Authentication with OpenId–Part 2
I my last post I created a very simple example of how to authenticate users with OpenId. In that example the user needed to enter the full URL to their OpenId provider. Today I’m going to modify that example to make the authentication process a little easier on the user. To give you an idea of what I’m going on about, take a look at the login form for StackOverflow.
Alternatively you can manually enter your OpenId (which is the only option I provided in the sample application).
The key to understanding how this works is to realize that for a user to authenticate with their OpenId provider they need to enter the URL pointing to that OpenId provider. However, for certain OpenId providers the URL will be the same for all users, while for others each user has a unique URL. For example, if you would like to login with your Google account you would enter the URL https://www.google.com/accounts/o8/id. (So in our previous example you could enter this as your OpenId URL) This URL is used for all Google accounts. However, if you’re using Blogger as your OpenId provider you need to enter http://youraccount.blogspot.com as the URL – the URL is different for every Blogger account. So if you want to login with your Google account you simply need to click the Google icon and you will be redirected to the single URL used for all Google accounts, but if you want to login with Blogger you need to be prompted for your account name.
OpenId Selector
OpenId Selector is an open source project that allows the user to select one of the popular OpenId providers and then either redirects them to the common URL for that provider or prompts the user for the necessary information and then redirects the user to their unique URL. This is all done with some JavaScript magic.
It’s incredibly straightforward to integrate OpenId Selector into your project. First, download the latest version from their site. You will need to include the 2 JavaScript libraries into your project. OpenId Selector uses jQuery so you will also need to reference that.
Also add the StyleSheet included with the project.
Now you need to include all the images. By default OpenId Selector assumes you put everything into a folder called Images, but I decided to put it into a folder called OpenId within my Content folder. There are 3 folders you need to copy – Images, Images.small and Images.large. You will also need to modify the openid-jquery.js file to specify the location of the images.
That’s pretty much all we need to do for the JavaScript and StyleSheet includes. Let’s take a look at the actual Html we need to include.
From looking at the StyleSheet and JavaScript it’s reasonably straightforward to figure out which of the Id’s you can change and which are required. In my case I only changed the name of the text input.
Lastly we simply need to invoke the JavaScript by passing the id of the text input.
And that’s it! Now the user can choose their OpenId provider and all of the code will still work on the server. I really am a fan of the OpenId Selector library. Make sure you play around with the different options available – you will need to edit the JavaScript file to do this.
Happy coding.