ASP.NET MVC 使用Forms Authentication 表單驗證登入 ... | asp.net mvc login example
ASP.NETMVC實作授權方式有兩種,一個是Identity2.0方式,另一種是使用FormsAuthentication,兩個方式都還有滿多工程師在用的。現在我們先使用FormsAuthentication的方式來實作會員登入。Step01.新增專案時,選擇無驗證方式,若選擇個別使用者帳戶時,專案範本就會預載Identity。Step02.在HomeController新增[Authorize],表示要授權才能進來[Authorize] publicclassHomeController:Controller { publicActionResultIndex() { returnView(); } } Step03.這時候如果你將網站跑起來,你將會得到HTTPErro...
ASP.NET MVC實作授權方式有兩種,一個是Identity 2.0方式,另一種是使用Forms Authentication,兩個方式都還有滿多工程師在用的。現在我們先使用 Forms Authentication 的方式來實作會員登入。
Step01.新增專案時,選擇無驗證方式,若選擇個別使用者帳戶時,專案範本就會預載 Identity。
Step02.在HomeController 新增 [Authorize],表示要授權才能進來
[Authorize] public class HomeController : Controller { public ActionResult Index() { return View(); } }Step03.這時候如果你將網站跑起來,你將會得到HTTP Error 401.0 – Unauthorized 的畫面
Step04.在Web.config 的 <system.web>中,增加 forms loginUrl 如下
<system.web> <authentication mode="Forms"> <forms loginUrl="/Account/Login" timeout="2880" /> </authentication> </system.web>Step05.這時候如果你將網站跑起來,你將會被導到登入的畫面
Step06.在專案新增資料夾 ViewModel,並建立 LoginViewModel.cs
public class LoginViewModel { [Required] public string UserName { get; set; } [Required] public string Password...