Custom Authentication and ASP.NET MVC

前端 未结 5 1389
独厮守ぢ
独厮守ぢ 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:15

    You can do the Session Authentication by simply putting a session variable value when the login is successful. Eg

    public ActionResult Index(Models.Login login)
        {
            if (ModelState.IsValid)
            {
                Dal.Login dLogin = new Dal.Login();
                string result = dLogin.LoginUser(login);
                if (result == "Success")
                    Session["AuthState"] = "Authenticated";
            }
            return View();
        }
    

    Now the trick is that you should have a common layout page of all the views to which you have to check for authentication. And in this layout page just do a razor check like this -

    
        @if (Session["AuthState"] != "Authenticated")
        {
            Response.Redirect("~/login");
        }
        // other html
    
    

    I have been using this method in my application admin panel.

提交回复
热议问题