diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-12-29 06:55:48 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-12-29 06:55:48 -0800 |
commit | 29d9694640f782d5ad0abc6550067b252a4ac194 (patch) | |
tree | 4deca5d9a09a3a751cb66d5cefec987dec6d3e81 | |
parent | ce35609a8d9faff87adad503a0e59dfb01c27e68 (diff) | |
download | DotNetOpenAuth-29d9694640f782d5ad0abc6550067b252a4ac194.zip DotNetOpenAuth-29d9694640f782d5ad0abc6550067b252a4ac194.tar.gz DotNetOpenAuth-29d9694640f782d5ad0abc6550067b252a4ac194.tar.bz2 |
In the MVC project template, OpenIdRelyingParty now has a wrapping interface to allow for easier unit testability.
I also made a rash of StyleCop fixes.
7 files changed, 129 insertions, 71 deletions
diff --git a/projecttemplates/MvcRelyingParty/Code/FormsAuthenticationService.cs b/projecttemplates/MvcRelyingParty/Code/FormsAuthenticationService.cs new file mode 100644 index 0000000..471c3250 --- /dev/null +++ b/projecttemplates/MvcRelyingParty/Code/FormsAuthenticationService.cs @@ -0,0 +1,36 @@ +namespace MvcRelyingParty { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Web; + using System.Web.Security; + using DotNetOpenAuth.OpenId; + + /// <summary> + /// Forms authentication interface to facilitate login/logout functionality. + /// </summary> + /// <remarks> + /// The FormsAuthentication type is sealed and contains static members, so it is difficult to + /// unit test code that calls its members. The interface and helper class below demonstrate + /// how to create an abstract wrapper around such a type in order to make the AccountController + /// code unit testable. + /// </remarks> + public interface IFormsAuthentication { + void SignIn(Identifier claimedIdentifier, bool createPersistentCookie); + + void SignOut(); + } + + /// <summary> + /// The standard FormsAuthentication behavior to use for the live site. + /// </summary> + public class FormsAuthenticationService : IFormsAuthentication { + public void SignIn(Identifier claimedIdentifier, bool createPersistentCookie) { + FormsAuthentication.SetAuthCookie(claimedIdentifier, createPersistentCookie); + } + + public void SignOut() { + FormsAuthentication.SignOut(); + } + } +} diff --git a/projecttemplates/MvcRelyingParty/Code/OpenIdRelyingPartyService.cs b/projecttemplates/MvcRelyingParty/Code/OpenIdRelyingPartyService.cs new file mode 100644 index 0000000..2300e48 --- /dev/null +++ b/projecttemplates/MvcRelyingParty/Code/OpenIdRelyingPartyService.cs @@ -0,0 +1,46 @@ +namespace MvcRelyingParty { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Web; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.RelyingParty; + + public interface IOpenIdRelyingParty { + IAuthenticationRequest CreateRequest(Identifier userSuppliedIdentifier, Realm realm, Uri returnTo); + + IAuthenticationResponse GetResponse(); + } + + /// <summary> + /// A wrapper around the standard <see cref="OpenIdRelyingParty"/> class. + /// </summary> + public class OpenIdRelyingPartyService : IOpenIdRelyingParty { + /// <summary> + /// The OpenID relying party to use for logging users in. + /// </summary> + /// <remarks> + /// This is static because it is thread-safe and is more expensive + /// to create than we want to run through for every single page request. + /// </remarks> + private static OpenIdRelyingParty relyingParty = new OpenIdRelyingParty(); + + /// <summary> + /// Initializes a new instance of the <see cref="OpenIdRelyingPartyService"/> class. + /// </summary> + public OpenIdRelyingPartyService() { + } + + #region IOpenIdRelyingParty Members + + public IAuthenticationRequest CreateRequest(Identifier userSuppliedIdentifier, Realm realm, Uri returnTo) { + return relyingParty.CreateRequest(userSuppliedIdentifier, realm, returnTo); + } + + public IAuthenticationResponse GetResponse() { + return relyingParty.GetResponse(); + } + + #endregion + } +} diff --git a/projecttemplates/MvcRelyingParty/Controllers/AccountController.cs b/projecttemplates/MvcRelyingParty/Controllers/AccountController.cs index 5ed2c53..48f2e53 100644 --- a/projecttemplates/MvcRelyingParty/Controllers/AccountController.cs +++ b/projecttemplates/MvcRelyingParty/Controllers/AccountController.cs @@ -17,38 +17,41 @@ [HandleError] public class AccountController : Controller { /// <summary> - /// The OpenID relying party to use for logging users in. + /// Initializes a new instance of the <see cref="AccountController"/> class. /// </summary> /// <remarks> - /// This is static because it is thread-safe and is more expensive - /// to create than we want to run through for every single page request. + /// This constructor is used by the MVC framework to instantiate the controller using + /// the default forms authentication and OpenID services. /// </remarks> - internal static OpenIdRelyingParty relyingParty = new OpenIdRelyingParty(); + public AccountController() + : this(null, null) { + } /// <summary> /// Initializes a new instance of the <see cref="AccountController"/> class. /// </summary> + /// <param name="formsAuth">The forms auth.</param> + /// <param name="relyingParty">The relying party.</param> /// <remarks> - /// This constructor is used by the MVC framework to instantiate the controller using - /// the default forms authentication and membership providers. + /// This constructor is not used by the MVC framework but is instead provided for ease + /// of unit testing this type. /// </remarks> - public AccountController() - : this(null) { - } - - // This constructor is not used by the MVC framework but is instead provided for ease - // of unit testing this type. See the comments at the end of this file for more - // information. - public AccountController(IFormsAuthentication formsAuth) { - FormsAuth = formsAuth ?? new FormsAuthenticationService(); + public AccountController(IFormsAuthentication formsAuth, IOpenIdRelyingParty relyingParty) { + this.FormsAuth = formsAuth ?? new FormsAuthenticationService(); + this.RelyingParty = relyingParty ?? new OpenIdRelyingPartyService(); } /// <summary> - /// Gets or sets the forms authentication module to use. + /// Gets the forms authentication module to use. /// </summary> public IFormsAuthentication FormsAuth { get; private set; } /// <summary> + /// Gets the OpenID relying party to use for logging users in. + /// </summary> + public IOpenIdRelyingParty RelyingParty { get; private set; } + + /// <summary> /// Gets the realm to report to the Provider for authentication. /// </summary> /// <value>The URL of this web application's root.</value> @@ -89,7 +92,7 @@ Identifier userSuppliedIdentifier; if (Identifier.TryParse(openid_identifier, out userSuppliedIdentifier)) { try { - var request = relyingParty.CreateRequest(openid_identifier, this.Realm, this.ReturnTo); + var request = this.RelyingParty.CreateRequest(openid_identifier, this.Realm, this.ReturnTo); request.SetUntrustedCallbackArgument("rememberMe", rememberMe ? "1" : "0"); // This might be signed so the OP can't send the user to a dangerous URL. @@ -128,12 +131,12 @@ /// </remarks> [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post), ValidateInput(false)] public ActionResult LogOnReturnTo() { - var response = relyingParty.GetResponse(); + var response = this.RelyingParty.GetResponse(); if (response != null) { switch (response.Status) { case AuthenticationStatus.Authenticated: bool rememberMe = response.GetUntrustedCallbackArgument("rememberMe") == "1"; - FormsAuth.SignIn(response.ClaimedIdentifier, rememberMe); + this.FormsAuth.SignIn(response.ClaimedIdentifier, rememberMe); string returnUrl = response.GetCallbackArgument("returnUrl"); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); @@ -160,35 +163,8 @@ /// </summary> /// <returns>The action result.</returns> public ActionResult LogOff() { - FormsAuth.SignOut(); + this.FormsAuth.SignOut(); return RedirectToAction("Index", "Home"); } } - - /// <summary> - /// Forms authentication interface to facilitate login/logout functionality. - /// </summary> - /// <remarks> - /// The FormsAuthentication type is sealed and contains static members, so it is difficult to - /// unit test code that calls its members. The interface and helper class below demonstrate - /// how to create an abstract wrapper around such a type in order to make the AccountController - /// code unit testable. - /// </remarks> - public interface IFormsAuthentication { - void SignIn(Identifier claimedIdentifier, bool createPersistentCookie); - void SignOut(); - } - - /// <summary> - /// The standard FormsAuthentication behavior to use for the live site. - /// </summary> - public class FormsAuthenticationService : IFormsAuthentication { - public void SignIn(Identifier claimedIdentifier, bool createPersistentCookie) { - FormsAuthentication.SetAuthCookie(claimedIdentifier, createPersistentCookie); - } - - public void SignOut() { - FormsAuthentication.SignOut(); - } - } } diff --git a/projecttemplates/MvcRelyingParty/Controllers/HomeController.cs b/projecttemplates/MvcRelyingParty/Controllers/HomeController.cs index 415bfe1..261aa37 100644 --- a/projecttemplates/MvcRelyingParty/Controllers/HomeController.cs +++ b/projecttemplates/MvcRelyingParty/Controllers/HomeController.cs @@ -1,10 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; +namespace MvcRelyingParty.Controllers { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Web; + using System.Web.Mvc; -namespace MvcRelyingParty.Controllers { [HandleError] public class HomeController : Controller { public ActionResult Index() { diff --git a/projecttemplates/MvcRelyingParty/Default.aspx.cs b/projecttemplates/MvcRelyingParty/Default.aspx.cs index 4ab779f..e9077cf 100644 --- a/projecttemplates/MvcRelyingParty/Default.aspx.cs +++ b/projecttemplates/MvcRelyingParty/Default.aspx.cs @@ -1,14 +1,13 @@ -using System.Web; -using System.Web.Mvc; -using System.Web.UI; +namespace MvcRelyingParty { + using System.Web; + using System.Web.Mvc; + using System.Web.UI; -namespace MvcRelyingParty { public partial class _Default : Page { public void Page_Load(object sender, System.EventArgs e) { // Change the current path so that the Routing handler can correctly interpret // the request, then restore the original path so that the OutputCache module // can correctly process the response (if caching is enabled). - string originalPath = Request.Path; HttpContext.Current.RewritePath(Request.ApplicationPath, false); IHttpHandler httpHandler = new MvcHttpHandler(); diff --git a/projecttemplates/MvcRelyingParty/Global.asax.cs b/projecttemplates/MvcRelyingParty/Global.asax.cs index 9761fb4..14772ae 100644 --- a/projecttemplates/MvcRelyingParty/Global.asax.cs +++ b/projecttemplates/MvcRelyingParty/Global.asax.cs @@ -1,13 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; -using System.Web.Routing; +namespace MvcRelyingParty { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Web; + using System.Web.Mvc; + using System.Web.Routing; -namespace MvcRelyingParty { - // Note: For instructions on enabling IIS6 or IIS7 classic mode, - // visit http://go.microsoft.com/?LinkId=9394801 + //// Note: For instructions on enabling IIS6 or IIS7 classic mode, + //// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { /// <summary> @@ -23,10 +23,9 @@ namespace MvcRelyingParty { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( - "Default", // Route name - "{controller}/{action}/{id}", // URL with parameters - new { controller = "Home", action = "Index", id = "" } // Parameter defaults - ); + "Default", + "{controller}/{action}/{id}", + new { controller = "Home", action = "Index", id = string.Empty }); } protected void Application_Start() { diff --git a/projecttemplates/MvcRelyingParty/MvcRelyingParty.csproj b/projecttemplates/MvcRelyingParty/MvcRelyingParty.csproj index e6bd0e3..2cc8360 100644 --- a/projecttemplates/MvcRelyingParty/MvcRelyingParty.csproj +++ b/projecttemplates/MvcRelyingParty/MvcRelyingParty.csproj @@ -59,6 +59,8 @@ <Reference Include="System.Web.Mobile" /> </ItemGroup> <ItemGroup> + <Compile Include="Code\FormsAuthenticationService.cs" /> + <Compile Include="Code\OpenIdRelyingPartyService.cs" /> <Compile Include="Controllers\AccountController.cs" /> <Compile Include="Controllers\HomeController.cs" /> <Compile Include="Default.aspx.cs"> |