MVC3 Loosly coupled authentication

376 views Asked by At

Out of the box MVC3 applications allow Windows Authentication when using the Intranet project template, or the Forms Authentication for an Internet project template. I've got a site that I'd like to use either. In addition, I've got an existing site that uses it's own custom type of authentication that authenticates users (no authorization or roles, just identification). I may need to use functionality of each, in addition to the data from the legacy system for authentication. Due to this, I'm trying to determine a way to abstract my authentication and decouple it. I'd like to use some kind of dependency injection, based entirely upon configuration, so I could deploy this same site in two different locations, and switch the authentication model (Windows Auth/Forms Auth/ Custom Auth), by changing configuration only.

Currently, all the ASP.NET applications I've worked with, including the MVC3 template projects, seem to be very tightly coupled with the authentication type used.

Am I thinking too far outside of the box on this one?

Is this possible, or is there a reason for this tight coupling?

UPDATE The real problem I have is between the existing legacy authentication I need to use for some users, versus the Forms Authentication I need for others. The Windows versus Forms authentication isn't really a problem, due to the LogIn form not being used for one. But consider the Custom Authentication and Forms Authenication. The LogIn form is tightly coupled to FormsAuthentication, more specifically to System.Web.Security. (i.e. Membership.ValidateUser, FormsAuthentication.SetAuthCookie, etc...).

I'd like to Inject into my AccountController the authentication to use, rather than having FormsAuthentication and Membership be used.

Does this make more sense in so far a what my problem is?

2

There are 2 answers

1
Craig Stuntz On

They're actually not so tightly coupled. The templates are just trying to get you up and running quickly.

ASP.NET membership supports both Forms and Domain auth.

In a site configured for Forms auth, e.g., you'll see a line in Web.config like:

<authentication mode="Forms">

You can change that to:

<authentication mode="Windows">

That's not the only difference (with Windows auth, e.g., you don't need a login page), but it's the most significant. You write your code based on the ASP.NET Membership API and only target Forms authentication in particular when you have to.

0
danludwig On

I agree with Craig's answer. The only thing I have to add is that I consider just about anything you can change in the web.config to be loosely coupled. The reason is that you can apply web.config transforms when you create a deployment package for your MVC app.

We use Unity for DI/IoC, and you can also specify your injection dependencies in web.config using Unity. You would just write your Web.Auth1.config to configure your app for one kind of authentication, and Web.Auth2.config to configure it for another kind of authentication. Then when you deploy, you just pick the target and VS builds the correct configuration for you.

If your source code needs to know which type of auth is used in the deployment, you could tell it with a web.config appSetting, which can also be changed with a web.config transform during deployment.