15833bfec3f934872d0e91d4d24b821da779cb07
Guides/MVC-Integration/Phase-II-Draft.md
| ... | ... | @@ -111,6 +111,107 @@ namespace Sample_MVCApp.Models { |
| 111 | 111 | ``` |
| 112 | 112 | ### Step 5. Add Controller |
| 113 | 113 | |
| 114 | + |
|
| 115 | + |
|
| 116 | +**a.** Right click on Controllers folder -> Add -> Controller -> Name it AccountController.cs -> Add |
|
| 117 | + |
|
| 118 | +**b.** Copy and paste the below code in AccountController.cs |
|
| 119 | + |
|
| 120 | +```csharp |
|
| 121 | +using System; |
|
| 122 | +using System.Collections.Generic; |
|
| 123 | +using System.Linq; |
|
| 124 | +using System.Web; |
|
| 125 | +using System.Web.Mvc; |
|
| 126 | +using Sample_MVCApp.Models; |
|
| 127 | +using System.Web.Security; |
|
| 128 | +using Izenda.AdHoc; |
|
| 129 | + |
|
| 130 | + |
|
| 131 | + |
|
| 132 | +namespace Sample_MVCApp.Controllers { |
|
| 133 | + public class AccountController : Controller { |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + // Login Action. This leads to login page |
|
| 138 | + |
|
| 139 | + public ActionResult Login() { |
|
| 140 | + return View(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Register Action. This leads to register view page |
|
| 144 | + public ActionResult Register() { |
|
| 145 | + return View(); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + // Post Register Action |
|
| 151 | + |
|
| 152 | + [HttpPost] |
|
| 153 | + public ActionResult Register(User user) { |
|
| 154 | + using (UserContext db = new UserContext()) { |
|
| 155 | + if (ModelState.IsValid) { |
|
| 156 | + db.Users.Add(user); |
|
| 157 | + db.SaveChanges(); |
|
| 158 | + return RedirectToAction("Login"); |
|
| 159 | + } |
|
| 160 | + else { |
|
| 161 | + ModelState.AddModelError("", "Some error has occured."); |
|
| 162 | + View(); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + return View(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + // Post Login Action |
|
| 173 | + |
|
| 174 | + [HttpPost] |
|
| 175 | + public ActionResult Login(User user) { |
|
| 176 | + using (UserContext db = new UserContext()) { |
|
| 177 | + try { |
|
| 178 | + if (ModelState.IsValid) { |
|
| 179 | + User us = db.Users.SingleOrDefault(m => m.Username == user.Username); |
|
| 180 | + if (us != null) { |
|
| 181 | + if (us.Password == user.Password) { |
|
| 182 | + FormsAuthentication.SetAuthCookie(user.Username, false); |
|
| 183 | + return RedirectToAction("Index", "Home"); |
|
| 184 | + } |
|
| 185 | + ModelState.AddModelError("", "Invalid Username or Password"); |
|
| 186 | + return View(); |
|
| 187 | + } |
|
| 188 | + throw new ArgumentException("User doesn't exists"); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + catch (ArgumentException e) { |
|
| 192 | + ModelState.AddModelError("", e.Message); |
|
| 193 | + return View(); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + return View(); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + //Log Out. |
|
| 202 | + [HttpPost] |
|
| 203 | + [ValidateAntiForgeryToken] |
|
| 204 | + public ActionResult LogOff() { |
|
| 205 | + FormsAuthentication.SignOut(); |
|
| 206 | + |
|
| 207 | + return RedirectToAction("Index", "Home"); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + } |
|
| 211 | +} |
|
| 212 | + |
|
| 213 | +``` |
|
| 214 | + |
|
| 114 | 215 | ### Step 6. Add Views |
| 115 | 216 | |
| 116 | 217 | ### Step 7. Add user role model |