添加控制器

在文件夹/ Controllers / Account 中创建一个新控制器。将文件命名为 MemberLoginSurfaceController.cs

using MyCMS.Models.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MyCMS.Controllers.Account
{
    public class MemberLoginSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
        // Inspired by: http://24days.in/umbraco/2012/creating-a-login-form-with-umbraco-mvc-surfacecontroller/

        // GET: MemberLoginSurface
        [HttpGet]
        [ActionName("MemberLoginForm")]
        [ChildActionOnly]
        public ActionResult MemberLoginForm()
        {
            return PartialView("Components/MemberLogin", new MemberLoginViewModel());
        }

        // The MemberLogout Action signs out the user and redirects to the site home page:
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult MemberLogout()
        {
            TempData["Message"] = "You have logged out!";
            Session.Clear();
            FormsAuthentication.SignOut();
            return RedirectToCurrentUmbracoPage();
        }

        // The MemberLoginPost Action checks the entered credentials using the standard Asp Net membership provider and redirects the user to the same page. Either as logged in, or with a message set in the TempData dictionary:

        [HttpPost]
        [ActionName("MemberLoginPost")]
        public ActionResult MemberLoginPost(MemberLoginViewModel model)
        {

            if (Membership.ValidateUser(model.Email, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
                return RedirectToCurrentUmbracoPage();
            }
            else
            {
                TempData["Status"] = "Invalid username or password";
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
}