我们正在尝试做一些有登录屏幕的网站。但是我们有一个问题。我们的域名是localhost/Login/User。但是,如果用户进入localhost/Home/Index,他/她可以不登录就可以访问我们的主站点。所以我们写了[授权]给我们的索引控制器。但我找不到我必须使用的东西。我必须在我们的项目中使用AuthorizeAttribute吗?MVC身份验证控制器
#Login Page
public class LoginController : Controller
{
//GET: Login
[IntranetAction]
public ActionResult Users()
{
return View();
}
public ActionResult Authentication(UserLoginInfo loginInfo)
{
bool isAuthenticated = new LdapServiceManager().isAuthenticated(loginInfo);
if (isAuthenticated)
{
//AUTHORIZED
Session["userName"] = loginInfo.username;
return Redirect("/Home/Index");
}
//WORNG PASSWORD, BACK TO LOGIN PAGE
TempData["message"] = "Yanlış kullanıcı adı ya da şifre";
return Redirect("/");
}
}
索引页
[Authorize]
public ActionResult Index()
{
Session["ip"] = Request.UserHostAddress;
if (IsDbExists())
{
_contactList = new List<Contact>();
UpdateOperations();
return View(_contactList);
}
Response.Redirect("/Loading/LoadingScreen");
return null;
}