Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Security;
- using BookingAppStore.Models;
- namespace BookingAppStore.Controllers
- {
- public class AccountController : Controller
- {
- UserContext dbU = new UserContext();
- public int getRoleId()
- {
- int roleid = 0;
- foreach (var u in dbU.Users)
- {
- if (u.Email == User.Identity.Name)
- {
- roleid = u.RoleId;
- break;
- }
- }
- return roleid;
- }
- public ActionResult Login()
- {
- ViewBag.Roleid = getRoleId();
- ViewBag.Username = User.Identity.Name;
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Login(LoginModel model)
- {
- if (ModelState.IsValid)
- {
- User user = null;
- using (UserContext db = new UserContext())
- {
- user = db.Users.FirstOrDefault(u => u.Email == model.Name && u.Password == model.Password);
- }
- if (user != null)
- {
- FormsAuthentication.SetAuthCookie(model.Name, true);
- ViewBag.Roleid = getRoleId();
- ViewBag.Username = User.Identity.Name;
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ModelState.AddModelError("", "Такого пользователя не существует");
- }
- }
- return View(model);
- }
- public ActionResult Register()
- {
- ViewBag.Roleid = getRoleId();
- ViewBag.Username = User.Identity.Name;
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Register(RegisterModel model)
- {
- if (ModelState.IsValid)
- {
- User user = null;
- using (UserContext db = new UserContext())
- {
- user = db.Users.FirstOrDefault(u => u.Email == model.Name);
- }
- if (user == null)
- {
- using (UserContext db = new UserContext())
- {
- db.Users.Add(new User { Email = model.Name, Password = model.Password, Age = model.Age, RoleId = 2 });
- db.SaveChanges();
- user = db.Users.Where(u => u.Email == model.Name && u.Password == model.Password).FirstOrDefault();
- }
- if (user != null)
- {
- FormsAuthentication.SetAuthCookie(model.Name, true);
- ViewBag.Roleid = getRoleId();
- ViewBag.Username = User.Identity.Name;
- return RedirectToAction("Index", "Home");
- }
- }
- else
- {
- ModelState.AddModelError("", "Пользователь с таким логином уже существует");
- }
- }
- return View(model);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement