all posts

Sample OpenID ASP.NET Web Site

Published to Blog on 8 Oct 2008

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

Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, motorcycles, sports, chickens, watches, food, Nashville, Savannah, Cincinnati and family.
Dan Hounshell on Twitter

  • On 9 Oct 2008 "TrackBack"" said:
  • On 10 Oct 2008 "Justin Kohnen"" said:
    Dude, that's sick awesome wicked! Hive five and congrats on your public mention.
  • On 15 Oct 2008 "Jeff"" said:
    Thanks a million Dan! This rocks. I wouldn't be too surprised to see the downloads start to climb here shortly.
  • On 15 Oct 2008 "Jeff"" said:
    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?
  • On 17 Oct 2008 "Dan Hounshell"" said:
    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.
  • On 17 Oct 2008 "Jeff"" said:
    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.
  • On 18 Oct 2008 "Jeff"" said:
    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?
  • On 19 Oct 2008 "Jeff"" said:
    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?
  • On 19 Oct 2008 "Jeff"" said:
    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?
  • On 9 Dec 2008 "Grant"" said:
    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.
  • On 9 Dec 2008 "Dan Hounshell"" said:
    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.
  • On 12 Dec 2008 "Grant"" said:
    Dan, Dude! You totally Rock! Yeah!!!! It Works!!!
  • On 6 Feb 2009 "Brandon"" said:
    The example doesn't seem to work anymore. It says "The compressed (zipped) file is invalid or corrupted."
  • On 21 Feb 2009 "Chris Charabaruk"" said:
    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.
  • On 19 Mar 2009 "Andrei Dimitriu"" said:
    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!