Project Description
Remember when the web was fun? Well it's back with WebMatrix. We are providing just a few minor helper extensions with a sample app that should help Neo on his quest to be
one with the Matrix.


This sample app provides a WebMatrixHelper file (yes, file) that you can use to separate your Razor Html files from your business logic. We do this using a little folder called App_Code - beautiful!

All of this

@{
    // Set the layout page and page title
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Log In";

    // Initialize general page variables
    var username = "";
    var password = "";
    var rememberMe = false;

    // Validation
    var isValid = true;
    var usernameErrorMessage = "";
    var passwordErrorMessage = "";

    // If this is a POST request, validate and process data
    if (IsPost) {
        username = Request.Form["username"];
        password = Request.Form["password"];
        rememberMe = Request.Form["rememberMe"].AsBool();

        // Validate the user's username
        if (username.IsEmpty()) {
            usernameErrorMessage = "You must specify a username.";
            isValid = false;
        }

        // Validate the user's password
        if (password.IsEmpty()) {
            passwordErrorMessage = "You must specify a password.";
            isValid = false;
        }

        // Confirm there are no validation errors
        if (isValid) {
            if (WebSecurity.UserExists(username) && WebSecurity.GetPasswordFailuresSinceLastSuccess(username) > 4 && WebSecurity.GetLastPasswordFailureDate(username).AddSeconds(60) > DateTime.UtcNow ) {
                Response.Redirect("~/account/AccountLockedOut");
                return;
            }

            // Attempt to login to the Security object using provided creds
            if (WebSecurity.Login(username, password, rememberMe)) {
                var returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl.IsEmpty()){
                    Response.Redirect("~/");
                } else {
                    Context.RedirectLocal(returnUrl);
                }
            }

            // If we arrived here, the login failed; convey that to the user
            isValid = false;
        }    
    }
}

Can be pulled out of your cshtml file and replaced with this:
@{this.Init(layout:"~/_SiteLayout.cshtml", title:"Log In", action:Account.Login.Init);}

Allowing you to again concentrate on your html without the bloat of the postback code.

Check out the samples today!

Last edited Feb 9, 2011 at 11:49 PM by hoffmanic, version 6