您的位置:首页 > 其它

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

2016-09-07 14:46 447 查看
  <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; }

     }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: