The "Home" controller had login and logout methods for logging into
the system. I used ASP.NET Forms Authentication so my login method
had stuff like creating the FormsAuthenticationTicket, creating a
cookie, adding it to the Response.Cookies collection, etc... My
Logout method called FormsAuthentication.SignOut(), Session.Abandon(),
cookie expiration. Also, the HttpApplication had code in the
AuthenticateRequest to determine if the user was already logged on --
again, by digging into cookie values and looking for stuff specific to
FormsAuthentication.
I had to abstract this authentication process into something I could
mock. So I created a simple authentication interface:
public interface IAuthenticationService
{
/// <summary>
/// Signs a user into the system.
/// </summary>
/// <param name="user"></param>
void SignIn(User user);
/// <summary>
/// Signs a user out of the system.
/// </summary>
void SignOut();
/// <summary>
/// Finds the currently logged in user (if any)
/// </summary>
/// <returns>The current user or <c>null</c> if no user was found.</returns>
User FindLoggedInUser();
}
NOTE: The "User" class above is a class in my application that
represents a user.
My Home controller constructor was changed to accept an instance of
IAuthenticationService (which is resolved at run-time by Windsor).
For unit testing, I can create a mock or sub for
IAuthenticationService. For run-time, I created a
FormsAuthenticationService and implemented IAuthenticationService.
This is where all of the old code got moved to (the stuff that dealt
directly with FormsAuthenticationTicket, FormsAuthentication.SignOut,
Session.Abandon, etc...
It worked out really well and I was really happy to get that
FormsAuthentication stuff out of my controller (plus, my login/logout
process now has some automated unit tests!).
--
Patrick Steele
http://weblogs.asp.net/psteele
Why not wrap the use of the UIP code in an interface?
Probably more of a ASP.NET 2.0 thing these days though.
On Tue, Sep 1, 2009 at 10:33, Tim Barcz<timb...@gmail.com> wrote:
> You referring to the HttpSimulator stuff? If so, +1