Add OpenID Login Support In Your ASP.NET Application

29. August 2010 14:50

API ASP.NET Visual Studio  15 Comments

Few days back I was looking at Scott Hanselman and friend's Open Source Nerddinner project, learning and grabbing some new things from there. Apart from looking some internals I looked at the OpenID implementation. This was all a week ago and now it's time for me to write and give some thoughts on DotNetOpenAuth. For folks who are new to OpenID I am going to demonstrate how you can implement one in your project with ease.

To get started download the DotNetOpenAuth API and extract the contents from the zip file. The download also includes some samples for you to get you started. Kickstart by creating a new ASP.NET webforms project. I am just using a default template for my application, you can use the one you like or start by creating a new page. Why I am using the default template for ASP.NET webform project? well you get to know later. So now when we are done creating a new ASP.NET webform project, time to add OpenID stuff in the project. Right click References  and select Add Reference. Browse to the bin folder where you have extracted the contents of the downloaded zip file. Select DotNetOpenAuth.dll and click OK.

Changing the web.config:

A minor change in the web.config is required. Find the below lines in your web.config file:

 <authentication mode="Forms"><forms loginUrl="~/Account/Login.aspx"
timeout="2880" /></authentication>

and replace it with:

<authentication mode="Forms"><forms defaultUrl="/default.aspx"
loginUrl="~/Account/login.aspx"/></authentication>

In the above configuration, loginUrl will always point towards to the login page and the defaultUrl will always point towards the page/URL which will be displayed when the authentication is successfull.

Implemeting OpenID support:

To implement OpenID support, open login.aspx page in the design view and then drag the OpenIdLogin from the toolbox on the page just after the default login implementation ends or where you want to have OpenID login. If you are not able to view the control, then you can browse the same dll you have add a reference in the above step.

If you want to implement this manually then register the control at the top of the page and then use it on the page.

Register the control:

<%@ Register Assembly="DotNetOpenAuth" Namespace="DotNetOpenAuth.OpenId.RelyingParty"
TagPrefix="rp" %>

To use the control on the page you can then simply write:

<rp:OpenIdLogin ID="OpenIdLogin1" runat="server"></rp:OpenIdLogin>

All done now and it's time to test our application. Hit F5 and login with your openID. The page rendered in front of you is

Enter your openID and click login. You will be redirected to the official OpenID login page

Enter your password for openID account and after the authentication is successfull you will be re-directed to the default page. Remember the web.config configuration we did!!? If everything goes well you can see your openID name login name on the top of the page.

This is the simplest way to implement openID. You can also implement openID on your ASP.NET MVC project to add some flexibility for you site users. Apart from this you can also download the Visual Studio 2010 template

Download: DOTNETOAUTH.zip (542.85 kb)

Related Links:

Currently rated 4.0 by 2 people

Comments (15)

Thanigainathan Thanigainathan
9/18/2010 12:40:52 AM #

Hi, Very nice article. Does this also provides roles and rights management ? Thanks, Thani

Vincent Vincent
9/19/2010 8:59:05 AM #

This article is very easy to read and accurate. Thank you!! It will be great if you show how to integrate this with the authentication system that comes with ASP.net and how to use it with roles, membership and profiles.

Rajan Rajan
9/21/2010 11:32:55 AM #

Hi, I am new to this concept. Can you explain the purpose of OpenID? Thanks, Rajan

Prashant Prashant
9/21/2010 1:15:04 PM #

I haven't tried it, but you can. There is a tutorial I came across a few days back on how to integrate openid with Membership, Roles and Profiles. http://www.eggheadcafe.com/tutorials/aspnet/4b3c7c9b-fe80-4e6e-a34e-0e9efed5c575/integrate-openid-authentication-with-aspnet-membership-roles-and-profile.aspx Hope this help!

Prashant Prashant
9/22/2010 1:23:11 AM #

@Vincent: The problem users are facing at the moment to use OpenID with roles, membership is that they are not able to get the other information from the user like his name, e-mail address etc. if the users logins with opened. I have figured out a way to get the user details and use those details with roles, membership and profiles. I will be blogging it on how you can do it shortly.

Jordon Jordon
9/24/2010 7:53:58 PM #

Nice article. One Quick Question After userlogin I want to add user details in my database. Do you know a way to implement this?

Prashant Prashant
9/24/2010 9:08:23 PM #

Yes, It's pretty simple. At the moment I am working on the same post. It will be available in few hours.

Prashant Prashant
9/26/2010 8:51:26 PM #

I have posted a new blog post on how to get user details and save it to database. Read more here http://midnightprogrammer.net/post/Extending-OpenID-Implementation-With-ASPNET-User-Information-Roles-and-Membership.aspx

suhail tufail suhail tufail
7/12/2012 9:02:12 PM #

I have a question , In open id function,could i find URL option?

Prashant Prashant
7/14/2012 11:23:44 AM #

What kind of URL you are referring to?

anil babu anil babu
8/23/2012 3:41:42 PM #

What is OAUTH ? How can i use this one in my ASP.NET webpages? plz help me Give me one Example on this one I new this concept plz help me

Prashant Prashant
8/23/2012 11:26:53 PM #

As you are not aware of what OAUTH is, I will recommend you to read about OAUTH here: http://oauth.net/about/ A more detail on Wikipedia: http://en.wikipedia.org/wiki/OAuth Once you get the idea what OAUTH is you can start with some basic tutorials.

anil babu anil babu
8/24/2012 3:22:42 PM #

Thank you so much, Give me some examples on ASP.NET webpages

sagor sagor
6/19/2016 8:05:38 PM #

What kind of URL you are referring to?

Prashant Prashant
6/26/2016 3:27:02 PM #

@sagor: There are 2 URLs I am using here. One is my account URL with OpenId and the other one is the redirect URL after the authentication is successfull.

Add Comment

Visit blogadda.com to discover Indian blogs Computers Blogs