将页面视图限制为仅管理员帐户?
Restrict page view to administrator account only?
我为一家虚构的私人飞机租赁公司创建了一个小项目。这是我的相关项目文件:
https://gist.github.com/anonymous/74d72f7b4c3c37257d16
在这个项目中,我有一个名为 MyMainDB.mdf
的数据库(在我的 App_data
解决方案文件夹下)。双击时,它会将我发送到服务器资源管理器,其中包含另一个名为 MyMainDBEntities
的文件,其中包含我的 tables 等(其中包含 SystemUsers
table)。
我已经完全实现并运行了注册/登录功能。当 运行 解决方案时,我还可以显示整个用户列表并从网站远程编辑该列表,但是,如何将对 AdminIndex
视图的访问限制为仅已登录且以 admin@admin.com
身份登录?
目前,一旦解决方案是 运行,用户就可以不受任何限制地导航到以下 URL:http://localhost:1921/MyTemplate/AdminIndex
。如果您未以管理员身份登录,我需要在代码方面添加什么以限制对页面的访问?任何人都可以向我展示我如何做到这一点的例子吗?一旦我知道如何做到这一点,我就可以将这些知识应用到我项目的其他关键方面
假设 MyTemplate
是您的控制器,您可以将 [authorize]
属性添加到您的控制器,如下所示
注:可设置为控制器级别或动作级别
[Authorize]
public class MyTemplateController : Controller //Controller level
或
[Authorize]
public ActionResult AdminIndex() //From the link mentioned above
{
return View(ss.SystemUsers.ToList());
}
在你的web.config
中添加这个
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
和您的登录 Post 操作方法使用 FormsAuthentication.SetAuthCookie
如下:
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
// Lets first check if the Model is valid or not
if (ModelState.IsValid)
{
// User found in the database
if (model.IsValid(model.Email, model.Password )) //According to the design you have in the link given above
{
FormsAuthentication.SetAuthCookie(username, false);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
这样就可以了。
有关详细信息,请访问 this link
我为一家虚构的私人飞机租赁公司创建了一个小项目。这是我的相关项目文件: https://gist.github.com/anonymous/74d72f7b4c3c37257d16
在这个项目中,我有一个名为 MyMainDB.mdf
的数据库(在我的 App_data
解决方案文件夹下)。双击时,它会将我发送到服务器资源管理器,其中包含另一个名为 MyMainDBEntities
的文件,其中包含我的 tables 等(其中包含 SystemUsers
table)。
我已经完全实现并运行了注册/登录功能。当 运行 解决方案时,我还可以显示整个用户列表并从网站远程编辑该列表,但是,如何将对 AdminIndex
视图的访问限制为仅已登录且以 admin@admin.com
身份登录?
目前,一旦解决方案是 运行,用户就可以不受任何限制地导航到以下 URL:http://localhost:1921/MyTemplate/AdminIndex
。如果您未以管理员身份登录,我需要在代码方面添加什么以限制对页面的访问?任何人都可以向我展示我如何做到这一点的例子吗?一旦我知道如何做到这一点,我就可以将这些知识应用到我项目的其他关键方面
假设 MyTemplate
是您的控制器,您可以将 [authorize]
属性添加到您的控制器,如下所示
注:可设置为控制器级别或动作级别
[Authorize]
public class MyTemplateController : Controller //Controller level
或
[Authorize]
public ActionResult AdminIndex() //From the link mentioned above
{
return View(ss.SystemUsers.ToList());
}
在你的web.config
中添加这个
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
和您的登录 Post 操作方法使用 FormsAuthentication.SetAuthCookie
如下:
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
// Lets first check if the Model is valid or not
if (ModelState.IsValid)
{
// User found in the database
if (model.IsValid(model.Email, model.Password )) //According to the design you have in the link given above
{
FormsAuthentication.SetAuthCookie(username, false);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
这样就可以了。
有关详细信息,请访问 this link