<authentication mode="Forms"> <forms loginUrl="~/Authentication/Login"></forms>

来源:互联网 发布:机械加工编程软件 编辑:程序博客网 时间:2024/04/28 09:58

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Authentication/Login"></forms>
    </authentication>


namespace MvcAppLesson4.Controllers
{
    public class HomeController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {        
            SalesERPDAL salesDal = new SalesERPDAL();
            List<Employee> employees = salesDal.Employees.ToList();
            return View("EmployeeListView", employees);

        }

        [Authorize]
        public ActionResult EmployeeListView()
        {
            SalesERPDAL salesDal = new SalesERPDAL();
            List<Employee> employees = salesDal.Employees.ToList();
            return View("EmployeeListView", employees);

        }



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppLesson4.Models;
using System.Web.Security;

namespace MvcAppLesson4.Controllers
{
    public class AuthenticationController : Controller
    {
        //
        // GET: /Authentication/

        public ActionResult Index()
        {
            return View();
        }

        //Authentication/Login
         // GET: Authentication
         public ActionResult Login()
         {
             return View();
         }

          [HttpPost]
          public ActionResult DoLogin(UserDetails u)
          {
              EmployeeBusinessLayer bal = new EmployeeBusinessLayer();
              if (bal.IsValidUser(u))
              {
                  FormsAuthentication.SetAuthCookie(u.UserName, false);
                  return RedirectToAction("Index", "HOME");
              }
              else
              {
                  ModelState.AddModelError("CredentialError", "Invalid Username or Password");
                  return View("Login");
              }
          }


    }
}


@model MvcAppLesson4.Models.UserDetails
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
</head>
<body>
    <div>
       @Html.ValidationMessage("CredentialError", new {style="color:red;" })
       @using (Html.BeginForm("DoLogin", "Authentication", FormMethod.Post))
       {

           @Html.LabelFor(c=>c.UserName)

           @Html.TextBoxFor(x=>x.UserName)

         

           <br />

           @Html.LabelFor(c => c.Password)

           @Html.PasswordFor(x => x.Password)

           <br />


           <input type="submit" name="BtnSubmit" value="Login" />

       }
      
    </div>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcAppLesson4.Models
{
    public class EmployeeBusinessLayer
    {
        public bool IsValidUser(UserDetails u)
        {
            if (u.UserName == "Admin" && u.Password == "Admin")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcAppLesson4.Models
{
     public class UserDetails
     {
         public string UserName { get; set; }
         public string Password { get; set; }
     }

}


0 0
原创粉丝点击