Tutorial: Sharing Authentication Between Community Server and Your ASP.NET Web Site

Introduction

In my last article I showed you how to setup your own ASP.NET 2.0 application to run as a virtual directory under Community Server 2.1. By the time we were finished, you were able to use the CommunityServer.Components library to display the currently logged in user's name.

Displaying a username is great and all, but what if you want to take advantage of Community Server's robust membership tools (built on top of the ASP.NET 2.0 membership system) to require users to login before accessing your newly created web application? In this article I will show you how to share authentication between Community Server and your own ASP.NET 2.0 web site.

Step 1: Prepare Community Server web site

Open the Web.config file at the root of your Community Server application. Locate the <authentication> tags and change the the loginURL of the <forms> tag from "login.aspx" to "/login.aspx". The authentication section should now look similar to this:

<forms name=".CommunityServer" protection="All" timeout="60000" loginUrl="/login.aspx" slidingExpiration="true" />

If you do not take the above step you will receive a "resource cannot be found" error. This is because your application will see "login.aspx" as a relative path and will attempt to find the login page at "/yourapp/login.aspx". The login.aspx file is actually at the root of your CS web site.

Step 2: Securing your ASP.NET application

We will start by preventing all anonymous users (users who are not logged into your Community Server web site) from accessing any portion of your web site.

Begin by opening the Web.config file at the root of your web site (not the Community Server site). Add the following XML between the <system.web> tags:

<!-- This keeps all anonymous users from accessing your site. -->
<authorization>
     <
deny users="?" />
</
authorization>

Make sure you are not logged into Community Server. Browse to your own application and you should be redirected to the Community Server Login page. After you login and you should be taken back to your web site.

Step 3: Give role specific access

Now, let's do something a little more fancy. Let's create a page that will only be accessable by users in the SystemAdministrator role.

Start by creating a new Web Form .aspx page at the root of your web site. Let's call it AdminsOnly.aspx. Add the text "If you can read this, you are a Systems Administrator." to the newly created page. Save the page and close it.

Next, open the Web.config at the root of your website (not the Community Server site). Add the following XML right above the bottom <configuration /> tag:

 <!-- This keeps out all users but those in the SysAdmin role. -->
<
location path="AdminsOnly.aspx">
   <
system.web>
      <
authorization>
         <
allow roles="SystemAdministrator"/>
         <
deny users="*"/>
      </
authorization>
   </system.web>
</
location>

If you have not already done so, create a user in your Community Server web site that is not in the SystemAdministrator role. Login as that user. Browse to your AdminsOnly.aspx page. You should receive a "User Already Logged In" message similar to the one pictured below:

Already Logged In Error Message

Now logout and log back in under your Community Server Systems Administrator account. Browse to your AdminsOnly.aspx page. The page should display normally, without the above pictured message.

Conclusion

You now know the basics of how to harness the power of Community Server's user authorization features within your own website. This should give you the information you need to share authentication between your ASP.NET 2.0 application and your Community Server website. Enjoy.

Published Thursday, November 02, 2006 9:05 AM by Tod Birdsall

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Community Server Daily News said:

news of the day a grab bag for what's happening in Community Server Rob Howard's BlogMailr tease post

November 2, 2006 4:00 PM
 

Daily News Faq List said:

Tod Birdsall with a beautifully timed (and well-written) article on sharing authentication between Community

November 22, 2006 10:01 AM
 

FAQs - Communityserver.Org said:

FAQ posts contain multiple subject items appropriate to this forum, and will evolve over time with new

January 9, 2007 10:39 PM
 

Pepinia said:

Looks like this works great and is remarkably easy. But what about the situation where CS is subweb to your larger existing web. In our existing web, we use an integer as user id and the entire web site except the login page requires log in. When we populated the CS database, we wrote some sql to populate the aspnet and the relevant cs prefixed users tables. We forced the identity column such that we can bring over our userid integer from the existing users. We don't like but we accept all of the overhead from the aspnet membership schema. The problem then becomes when our users log in at our default login page, they are not able to share authentication with the CS web because the CS web uses the GUID from the membership table instead of the user id. I am currently seeking any ideas on how to make this work. I am not even sure which direction to go with this.
March 5, 2007 12:55 PM
 

Tod Birdsall said:

Hi Pepinia,

I believe that you can get the integer unique user ID using the following code:

CommunityServer.Components.CSContext.Current.User.UserID;

March 5, 2007 1:00 PM
 

Community Server Bits said:

Tod Birdsall with a beautifully timed (and well-written) article on sharing authentication between Community

March 12, 2007 7:27 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit