Custom Authentication and ASP.NET MVC

前端 未结 5 1382
独厮守ぢ
独厮守ぢ 2020-12-13 00:50

I have an internal web app being built in ASP.NET 4. We are stuck with using an authentication API built by another team. If a user to the site is authenticated

5条回答
  •  情深已故
    2020-12-13 01:17

    You can use Forms Authentication in conjuction with Authorize attibute as follows,

    To restrict access to a view :

    Add the AuthorizeAttribute attribute to the action method declaration, as shown below,

    [Authorize]
    public ActionResult Index()
    {
        return View();
    }
    

    Configuring Forms Authentication in web.config

    
         
    
    

    Login Post Action: Set Authentication cookie if user is valid

    [HttpPost]
    public ActionResult Login(User model, string returnUrl)
    {
            //Validation code
    
            if (userValid)
            {
                 FormsAuthentication.SetAuthCookie(username, false);
            }
    }
    

    Log off Action:

    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
    

提交回复
热议问题