Pages

Tuesday, November 23, 2010

How to add OpenID to your ASP.NET web site (in C# or VB.NET)

Adding OpenID support to your VB.NET web site couldn't be easier.  Here is the easiest way:
  1. Download the DotNetOpenId library.
  2. Extract the DotNetOpenId.dll from the bin directory of the .zip file you downloaded.
  3. Make sure you're using FormsAuthentication.
    1. Open Web.config
    2. Find your tag (or create it if you're sure it doesn't already exist) and change it if necessary so it looks something like this: (the really important part is just mode="Forms")
      <authentication mode="Forms">
          <forms defaultUrl="/default.aspx" loginUrl="~/login.aspx"/>
      authentication>
  4. Add a Reference to DotNetOpenId.dll from your web site.
  5. Navigate to your Login.aspx page.
  6. Drag the OpenIdLogin control from your control Toolbox to the location on your page you want it to appear. NOTE: If the controls do not appear in your Toolbox, follow these steps to add them:
    1. Right-click somewhere on the Toolbox and click Choose Items... (it can take a while before the dialog shows up)
    2. In the .NET Framework Components tab, choose Browse.
    3. Select the DotNetOpenId.dll that you extracted from the zip file.
    4. Click OK.
    5. Several controls will get added to your Toolbox.  Drag the OpenIdLogin control to your page.
  7. You should have a couple of new lines in your .aspx file:
    <%@ Register Assembly="DotNetOpenId" Namespace="DotNetOpenId.RelyingParty" TagPrefix="RP" %>
    <RP:OpenIdLogin ID="OpenIdLogin1" runat="server" />
  8. If you're starting a brand new site and don't have a way to tell who is logged in on your site yet, go ahead and drag LoginName and LoginStatus controls to some new page that you can use to check your own logged-in status.
  9. Go ahead and run your app.  You're already done.
Now, this is admittedly the simplest case.  If you have an existing userbase with usernames and passwords, you'll want to build pages to help your users make the transition.  You can certainly support dual-mode authentication, but each web site is different and you'll have to design your own plan for doing that.  But if you find you need more flexibility with how the OpenID login control looks or works, check out the OpenIdTextBox control.  If there's demand (leave a comment on this post), I'll follow up with a post or two on how to customize this.

No comments: