【发布时间】:2016-09-01 04:27:37
【问题描述】:
我们正在尝试做一些有登录屏幕的网站。但是我们有一个问题。我们的域是 localhost/Login/User。但是如果用户输入 localhost/Home/Index,他/她可以在没有登录的情况下访问我们的主站点。所以我们将 [Authorize] 写入我们的索引控制器。但我找不到我必须使用什么。我必须在我们的项目中使用 AuthorizeAttribute 吗?
#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;
}
如何在我的 LoginController/Authentication 函数中访问索引
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 authentication