ASP Authetification Authorize adding

时间:2016-06-18 20:03:01

标签: asp.net-mvc

I need to add authorize to my project knowing that the authentification work well

This is my Login method :

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(User u)
    {
            var v = db.users.Where(a => a.Email.Equals(u.Email) && a.Password.Equals(u.Password) ).FirstOrDefault();
            if (v !=null)
            {
               int priv=v.PrivilegeID;
                Session["LoginuseID"] = v.UserID.ToString();
                Session["Loginusename"] = v.Nom.ToString();
                if ( priv == 1)
                    return RedirectToAction("index", "Home");
                else
                    return RedirectToAction("indexuser", "Home");

            }
            else
            {
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(u);
            }

        //return View();
    }

Thanks in advance

1 个答案:

答案 0 :(得分:0)

You can create a BaseController which derives from the Controller class, Annotate it with the Authorize Attribute and then extend the AppController in Other Controllers in your application, for the methods where you don't require Authorization or Authentication use the [AllowAnonymous] Attribute

[Authorize]
public class AppController : Controller
{

}


public class HomeController : AppController 
{
   [AllowAnonymous]
   public ActionResult Login { return View();}
}