Add OpenID Login Support In Your ASP.NET Application

by prashant 29. August 2010 23:50

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:

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , , ,

API | ASP.NET | Visual Studio


Comments (8) -

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

Hi,

Very nice article. Does this also provides roles and rights management ?

Thanks,
Thani

Reply

Prashant
Prashant India
9/21/2010 10: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.

www.eggheadcafe.com/.../...-roles-and-profile.aspx

Hope this help!

Reply

Vincent
Vincent United States
9/19/2010 5:59:05 PM #

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.

Reply

Rajan
Rajan India
9/21/2010 8:32:55 PM #

Hi,

I am new to this concept. Can you explain the purpose of OpenID?

Thanks,
Rajan

Reply

Prashant
Prashant India
9/22/2010 10: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.

Reply

Jordon
Jordon United States
9/25/2010 4:53:58 AM #

Nice article.

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

Reply

Prashant
Prashant India
9/25/2010 6:08:23 AM #

Yes, It's pretty simple. At the moment I am working on the same post.

It will be available in few hours.

Reply

Prashant
Prashant India
9/27/2010 5:51:26 AM #

I have posted a new blog post on how to get user details and save it to database. Read more here

midnightprogrammer.net/.../...-and-Membership.aspx

Reply

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Visit blogadda.com to discover Indian blogs Computers Blogs

About Me

Name of authorPrashant Khandelwal.
Programmer and tech enthusiast. More...

Feeds Subscribe Twitter Facebook Google Plus Linked In Delicious

Badges

MVB

MVP Blog Badge.

HTML5 Powered with CSS3 / Styling, Graphics, 3D & Effects, Multimedia, Performance & Integration, Semantics, and Offline & Storage

Month List

Blog Stats

414,237 Hits

Adverts

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Creative Commons License