Digging My Blog - Dan Hounshell

Another .Dan Powered Site

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

 

Posted: Oct 08 2008, 11:31 PM by Dan Hounshell | with 12 comment(s)
Filed under:

Comments

TrackBack said:

# October 9, 2008 10:35 AM

Justin Kohnen said:

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

# October 10, 2008 7:47 AM

Jeff said:

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

# October 15, 2008 10:07 AM

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?

# October 15, 2008 10:20 AM

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.

# October 17, 2008 8:11 AM

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.

# October 17, 2008 5:49 PM

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?

# October 18, 2008 1:08 PM

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?

# October 19, 2008 8:16 AM

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?

# October 19, 2008 12:30 PM

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.

# December 9, 2008 6:54 PM

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.

# December 9, 2008 9:18 PM

Grant said:

Dan,

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

# December 12, 2008 5:52 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Please type in: hello world