I’m trying to make a Restaurant WebSite and I wanted to make Admin and Customer. Admin can create new foods that can be seen on the website or delete etc. Customer can only buy foods.
So I made a test database and it looks like this:
userName | password | rank |
---|---|---|
user1 | user01 | 1 |
user2 | user02 | 0 |
and made SignIn, SignUp and Logout pages and here is the codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tst_MVC1.Models;
using System.Web.Security;
namespace tst_MVC1.Controllers
{
[AllowAnonymous]
public class SecurityController : Controller
{
DenemeSQLEntities db = new DenemeSQLEntities();
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(tstTable user)
{
var userInDb = db.tstTable.FirstOrDefault(x => x.userName == user.userName && x.password == user.password);
if (userInDb != null)
{
FormsAuthentication.SetAuthCookie(user.userName, false);
return RedirectToAction("Index", "Home");
}
else
{
ViewBag.Mesaj = "Geçersiz Kullanıcı Adı veya Şifre";
return View();
}
}
public ActionResult SignUp()
{
return View();
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Login");
}
[HttpPost]
public ActionResult SignUp(FormCollection form)
{
DenemeSQLEntities db = new DenemeSQLEntities();
tstTable model = new tstTable();
model.userName = form["userName"].Trim();
model.password = form["password"].Trim();
db.tstTable.Add(model);
db.SaveChanges();
return RedirectToAction("Login");
}
}
}
and I want to do when someone trying to login, Controller needs to check it’s Rank and if its 1 it should show admin pages to it but its Rank below 1 it should show the Customer pages.
I thought i can get the Rank from the database and check like this but i cant get the Rank from the database:
if (userInDb != null)
{
if (rank == 1)
{
FormsAuthentication.SetAuthCookie(user.userName, false);
return RedirectToAction("Index", "Home"); //its for test i wont send it to index page
}
else
{
FormsAuthentication.SetAuthCookie(user.userName, false);
return RedirectToAction("About", "Home"); //"About" is for test.
}
}
else
{
ViewBag.Mesaj = "Geçersiz Kullanıcı Adı veya Şifre";
return View();
}