<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; }
}
}
<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; }
}
}
相关文章推荐
- 关于<authentication mode="Forms" />
- ASP.NET&C#学习笔录4(<authentication mode="Windows|Forms|Passport|None"> )
- <authentication+mode="Forms">+是应用级别的...在子目录下用+<authorization>节来控制访问的解决方法
- <authentication+mode="Forms">+是应用级别的...在子目录下用+<authorization>节来控制访问的解决方法
- <authentication mode="Forms"> 发布错误
- <authentication mode="Forms"> 发布错误
- Web应用程序中的服务器错误 (<authentication mode="Forms">)
- 出现<authentication mode="Windows"/>错误解决办法
- ASP.NET提示<authentication mode="Windows" /> 错误
- IIS 提示 <authentication mode="Windows" /> 错误
- <authentication mode="Windows"/>
- 出现<authentication mode="Windows"/>错误解决办法
- 出现<authentication mode="Windows"/>错误解决办法
- <input style="ime-mode:disabled">
- Jquery对<s:radio list="#{1:'是',0:'否'}" name=""></s:radio>清空设置
- <%@page contentType="text/html;charset=gbk"%>与<meta http-equiv="Content-Type" content="text/html; ch
- <script>alert("s")</script>
- <a href="" />,<a href="#" />与<a href="javascript:void(0)" />的区别
- <item name="android:windowNoTitle">true</item> 不起作用。标题栏未被隐藏
- <srcipt src="/socket.io/socket.io.js"></script>加载的资源是从哪儿冒出来的?