ASP.NET Authentication – Multiple Domains w/ Same Application


B03B0021

In our series about ASP.NET authentication, so far we’ve covered all the rather normal cases where you’d want to have the ability to log into different domains attached to the same application?  There are a few additional hurdles you’ll need to overcome to make this work correctly.

The first is, how do I let each domain have there own unique set of logins?

You see, while you can use the same database for multiple domains spread across different applications.  The usernames are application specific so that if you log in with domain1.com and domain2.com both pointing to the same domain, either will work.  Normally, this is what we want to have happen.

But if those two domains really represent two different web sites, we are just using the same core code, things get a little trickier.

The easiest way to deal with this problem is to make the usernames unique prior to registering and prior to authenticating.  This will mean you’ll need to trap some events.

To create a new user and make the username unique, trap the CreatingUser event on the new user wizard.

At this point, there are a couple choices.  You can just change the username.  I suggest pre-pending the username with the domain.  Or you can call the registration APIs directly.

To change the username use this code.

m_createUserWizard.UserName =
    "domain.com_" + m_createUserWizard.UserName;

If you want to just call the APIs, you’ll want to call CreateUser();


MembershipCreateStatus status;
Membership.CreateUser(m_createUserWizard.UserName,
    m_createUserWizard.Password,
    m_createUserWizard.Email,
    m_createUserWizard.Question,
    m_createUserWizard.Answer,
    true,out status);
e.Cancel = true;
if (status == MembershipCreateStatus.Success)
     e.Cancel = true;
else
{
    // display errors.
}

To login you’ll need to trap the LoggingIn event and the LoginError event.

In the LoggingIn event, change the username

m_login.UserName = "domain.com_" + m_login.UserName;

In the LoginError, you’ll want to change it back so that it displays correctly if there was an error.

m_login.UserName = m_login.UserName
    .Replace("domain.com_", string.Empty);

or something equally as effective.

Ads by Lake Quincy Media

Other Related Items:

Sneaker EZ 4 oz Spray/ Eliminates Sneaker Odor/ Highly Concentrated/ Proven Effective/ Allows Sneaker to Breathe Properly/ Hundreds of Applications in Each Bottle/ Really WorksSneaker EZ will eliminate and control sneaker odor between washings. It is important that sneakers breathe. Sneaker EZ is a non powder formulation tha... Read More >
Don't Mess With Me... Programmer on Women's Cotton T-Shirt (in 22 colors)Don't Mess With Me... Programmer on Women's Cotton T-Shirt (in 22 colors)100% preshrunk heavyweight cotton; double-needle stitching throughout; seamless rib at neck; shoulder-to-shoulder tape; heather grey is 90% cotton, 10% polyester; fashion cut; 5/8" rib collar; fitted tapered sleeve.
Prepac Cherry 60" Flat Panel LCD/Plasma TV ConsoleThe Cherry Finish 60 Inch Plasma TV Stand from Prepac offers ample storage for DVDs, VHS tapes and CDs, room for your electronic components as well as... Read More >

Related Post

3 Responses to “ASP.NET Authentication – Multiple Domains w/ Same Application”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor