Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

NAVIGATION - SEARCH

ASP.Net MVC Membership Starter Kit

ASP.Net MVC Membership starter kit Yesterday, I read a cool blog post from Troy Goode about his new CodePlex project MvcMembership. I also noticed his call for help, so I decided to dedicate some of my evening hours to his project.

Almost every (ASP.NET) website is using some form of authentication, in most cases based on ASP.NET membership. With this in mind, Troy started an ASP.NET MVC version of this. The current release version provides a sample application containing some membership functionality:

  • FormsAuthenticationController featuring:
    • Register action & view
    • Login action & view
    • Logout action
  • FormsAuthenticationAdministrationController featuring:
    • User List action & view
    • User Detail action & view
    • Role List action & view
    • Create User action & view
    • Change/Reset Password actions
    • Role Management actions
  • Custom Action Filters, including:
    • a RequiresAnonymousAttribute
    • a RequireAnyRoleAttribute
    • a RequireEveryRoleAttribute
    • a RedirectToActionOnErrorAttribute
    • a RedirectToUrlOnErrorAttribute

After an evening of contributing code, there's additional functionality in the source control system:

  • FormsAuthenticationController featuring:
    • Reset password action & view
    • Retrieve password action & view
    • Change password action & view

Also, I've been doing some massive refactoring to this project. Everything that is "generic" for most applications is now stripped out in a separate assembly still allowing situation-specific overrides. For example, if you use this MvcMembership framework, you can simply inherit the BaseFormsAuthenticationController class in your own code:

[code:c#]

namespace MvcMembership.Controllers
{
    public class FormsAuthenticationController : StarterKit.Mvc.Membership.Controllers.BaseFormsAuthenticationController
    {
    // Nothing here... All is handled in the BaseFormsAuthenticationController class!
    }
}

[/code]

Need a custom Login action? No problem!

[code:c#]

namespace MvcMembership.Controllers
{
    public class FormsAuthenticationController : StarterKit.Mvc.Membership.Controllers.BaseFormsAuthenticationController
    {
        public override void Login()
        {
            // this is an override, additional ViewData can be set here
            base.Login();
        }
    }
}

[/code]

Want to respond to some actions inside BaseFormsAuthenticationController? No problem either!

[code:c#]

namespace MvcMembership.Controllers
{
    public class FormsAuthenticationController : StarterKit.Mvc.Membership.Controllers.BaseFormsAuthenticationController
    {
        public override void OnAfterResetPassword(string email, string userName, string newPassword)
        {
            // TODO: replace with sender e-mail address.
            MailMessage mailMessage = new MailMessage("sender@example.com", email);

            // TODO: replace with custom subject.
            mailMessage.Subject = "Your password";

            // TODO: replace with custom body.
            mailMessage.Body = string.Format("{0}, your password is: {1}.", userName, newPassword);

            // TODO: replace with the name of your SMTP server.
            SmtpClient smtpClient = new SmtpClient("localhost", 25);
            smtpClient.Send(mailMessage);
        }
    }
}

[/code]

Let's hope the ASP.NET MVC team picks this up, as I think it's something lots of users would like to see. For now, it's a separate download from CodePlex.

kick it on DotNetKicks.com  

blog comments powered by Disqus