ASP.NET MVC5 表單驗證
FormsAuthentication.SetAuthCookie(Data.Name, false);
<system.web>
<!--<authentication mode="None" />-->
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="~/Account/Login" defaultUrl="Account/Login" timeout="120" cookieless="UseDeviceProfile" />
</authentication>
</system.web>
<system.webServer>
<!--<modules>
<remove name="FormsAuthentication" />
</modules>-->
</system.webServer>
[Authorize]
public class AccountController : Controller
{
// GET: Account
[HttpGet]
[AllowAnonymous]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(Login Data)
{
if (Data.Name != null)
{
FormsAuthentication.SetAuthCookie(Data.Name, false);
return RedirectToAction("Index");
}
return RedirectToAction("Login");
}
public ActionResult Index()
{
ViewBag.stauts = "未登入狀態";
if (User.Identity.IsAuthenticated)
{
ViewBag.stauts = "您現在是已登入狀態";
ViewBag.Name = User.Identity.Name;
}
return View();
}
[AllowAnonymous]
public ActionResult LoginOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Login");
}
}
public class Login
{
[Required]
[Display(Name = "帳號")]
public string Name { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "密碼")]
public string Password { get; set; }
}
以上內容,若有錯誤
煩請各路高手路過指正
謝謝!
<(_ _)>