October 08, 2008

Sample OpenID ASP.NET Web Site

A couple of months ago I posted how to add OpenID to your existing web site in conjunction with ASP.NET Membership. In the comments a few times I was asked to post the code to the OpenID login user control or to create a sample project to share. Even though there really isn't a lot to the code other than what I shared in that article I said that I would post the sample code. After ScottGu linked to my original post last week I felt compelled to step up the pace of doing so.

Below is a link to the sample solution/project/web application. There is really nothing to the web site itself, just a static home page, a "join" page and a "login" page that use traditional membership controls and the OpenID login user control. Go ahead and check out the code and feel free to use that control and the accompanying utility code in your own projects. Please read the Readme.txt file included in the zip for setup instructions.

Sample OpenID Web Site

 

Comments,

  • Trackbacks,
  • and Pingbacks
  1. Dude, that's sick awesome wicked! Hive five and congrats on your public mention.

  2.  avatar Jeff says:

    Thanks a million Dan! This rocks. I wouldn't be too surprised to see the downloads start to climb here shortly.

  3.  avatar Jeff says:

    Dan,

    Two questions. One, I notice that it looks like this is a VS 2008 website. Will this still work with a website made using  VS 2005, or Visual Web Developer Express 2005?

    Is this just for use with Sql databases or can it be used with any database like MySql where the Membership and Roles pieces have been ported?

  4. Jeff, great questions. It is a VS 2008 web application project. However, there is nothing VS 2008 specific. It should be easy enough to create a new VS 2005 web application project/solution, add the files, and be good to go. I can't think of any reason why it wouldn't work as a web site in either VS 2005 or VWD Express. There may need to be some tweaking to change the .cs files to code behind files (or code beside or whatever the terminology is now) - it's been a while since I've done that so I'm not sure on the details.

    The only database access that is done is through the membership provider. As long as you have a membership provider for MySQL then that should be fine.

  5.  avatar Jeff says:

    Dan,

    Thanks for that. I was pretty sure that was how it was but wanted to double check before I got too far into development. So far I have found two sources for using ASP.NET membership with a MySql database so I'll be checking into those next.

  6.  avatar Jeff says:

    Dan,

    I've been trying for two days now to get this to work and I'm just not having much luck. I keep getting a NullReferenceException not handled by user code at Line 29 of OpenIdLoginForm.ascx.cs.

    ClaimsResponse fetch = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;

                           alias = fetch.Nickname;

                           email = fetch.Email;

    Line 29 is the one that says alias = fetch.Nickname.

    Any advice on how to fix this?

  7.  avatar Jeff says:

    Dan,

    I solved that problem above witht he failed nickname, I didn't have a persona defined in my OpenId Account, but it would seem even though I now get a successful logn into my OpenId account I still get a "Login failed: Unsuccessful creation of Account: InvalidPassword" error. Any advice on chasing this one down?

  8.  avatar Jeff says:

    Dan,

    Okay, I finally got it to work. I found that I usually request a minimum 8 character password with 1 NonAlphaNumeric character.

    You have it working with a minimum of 5 characters and no NonAlphaNumeric characters. Thus the password was never valid. Any advice on how to work adjust what you have so that it accounts for the 1 nonalphanumeric character?

  9.  avatar Grant says:

    Dan, I'm trying to get your sample app working in VS 2005 and I have everything except Extensions.cs working.

    How do I need to change Extensions.cs so it works with a VS 2005 website.

  10. Grant, the method in that file is an Extension Method, which is used to extend the existing MembershipUser class with new functionality. I believe that Extension Methods were introduced in .NET 3.5. So you will not get it to work as is with VS 2005.

    However, you should be able to easily refactor it to just a normal static utility-type method to do the same thing as the Extension method did.

    In fact try changing the code to this and it should work...

    public static string DisplayName(MembershipUser user)

    {

       string retval = user.UserName;

       if (user != null) {

           if (!string.IsNullOrEmpty(user.Comment)) {

               retval = user.Comment;

           }

       }

       return retval;

    }

    then change the code in the one place that calls it, the SiteMaster.cs file from:

    return user.DisplayName();

    to:

    return Components.Extensions.DisplayName(user);

    That should be all you need to do.

  11.  avatar Grant says:

    Dan,

    Dude! You totally Rock! Yeah!!!! It Works!!!

  12.  avatar Brandon says:

    The example doesn't seem to work anymore.  It says "The compressed (zipped) file is invalid or corrupted."

  13. It'd be great to have an MVC equivalent of this sample. On the other hand, I'm sure I can figure out what I need from this.

    @Brandon Works for me, using WinRAR. Try re-downloading or perhaps alternate compression/decompression software.

  14. Hey Dan,

    Firstly thank you very much for your code and ur knowledge! Helped me a lot!

    Im just having the same problem as Jeff. He said he didnt define a persona on his Openid profile. I dont know what that means please can any1 help me?

    Thanks!

Comments are closed.

 

Trackbacks and Pingbacks


  1. TrackBack says:
Shortcuts

Where is Dan?


My Blog
My Blog
Twitter
Twitter
Facebook
Facebook
LinkedIn
LinkedIn
Flickr
Flickr
YouTube
YouTube
Delicious
Delicious
Foursquare
Foursquare
Pinterest
Pinterest
GetGlue
GetGlue
Pintley
Pintley
XBOX Live
XBOX Live
Last.fm
Last.fm
Windows Live
Windows Live
Telligent.com
Telligent
Graffiti CMS on CodePlex
Graffiti CMS
Popular

Recent Posts