[.net] MVC에서 web.config 사용해서 Login 로직 만들기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
1. Edit ~\web.config to include the following forms-based authentication configuration.
1 2 3 4 5 | <system.web> <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="30" /> </authentication></system.web> |
2. Register AuthorizeAttribute in ~\App_Start\FilterConfig.cs.
1 | filters.Add(new AuthorizeAttribute()); |
3. Add view model LogOnViewModel in ~\Models\Account\LogOnViewModel.cs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class LogOnViewModel{ [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [Display(Name = "Remember me?")] public bool RememberMe { get; set; }} |
4. Add controller AccountController and LogOn action methods for both HttpGet & HttpPost.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class AccountController : Controller{ // // GET: /Account/LogOn [AllowAnonymous] public ActionResult LogOn() { LogOnViewModel model = new LogOnViewModel(); return View(model); } // // POST: /Account/LogOn [AllowAnonymous] [HttpPost] public ActionResult LogOn(LogOnViewModel model, string returnUrl) { if (this.ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (this.Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } // If we got this far, something failed, redisplay form this.ModelState.AddModelError("", "Incorrect user name or password."); return View(model); } // // POST: /Account/LogOff [HttpPost] public ActionResult LogOff() { FormsAuthentication.SignOut(); return RedirectToAction("Index", "Home"); }} |
5. Add view in ~\Views\Account\LogOn.cshtml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @model SecurityApp.Models.Account.LogOnViewModel@{ Layout = null; ViewBag.Title = "Log On"; ViewBag.ReturnUrl = Request["ReturnUrl"];}<!DOCTYPE html><html>......<body> <h2>@ViewBag.Title</h2> @using (Html.BeginForm(null, null, new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true)<br /> @Html.TextBoxFor(m => m.UserName, new { placeholder = Html.DisplayNameFor(m => m.UserName) })<br /> @Html.PasswordFor(m => m.Password, new { placeholder = Html.DisplayNameFor(m => m.Password) })<br /> @Html.CheckBoxFor(m => m.RememberMe) @Html.DisplayNameFor(m => m.RememberMe)<br /> <button type="submit">Log On</button> } ......</body></html> |
6. In ~\Views\Shared\_Layout.cshtml, add a HTML form to handle log off.
1 2 3 4 | @using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logOffForm" })){ @Html.AntiForgeryToken()} |
7. In ~\Views\Shared\_Layout.cshtml, add a hyperlink to log off.
1 | <a href="javascript:$('#logOffForm').submit()">Log Off</a> |
'Web > ASP.NET MVC' 카테고리의 다른 글
| [Asp.net] Json 을 MVC controller 로 Posting 하는 방법 (2) | 2015.04.27 |
|---|---|
| [ASP.net] Json 문자열을 컨트롤로로 보내고 deserialize 하는 방법 (2) | 2015.04.27 |
| [.net] ASP.NET 파일다운 처리시 한글파일명 깨짐현상 해결 (2) | 2015.04.16 |
| [.net] MVC ASP.net 엑셀 익스포트 시 엑셀 파일내에 한글깨짐 방지 (2) | 2015.04.16 |
| [.net] MVC 컨트롤러에서 view상에 alert 띄우기 (2) | 2015.04.16 |