[英]MVC 3 location allow users in web.config
我正在尝试通过使用web.config进行授权。 在我的用户注册中,它没有使用ASP.NET配置。 我正在处理数据库登录页面。 我想通过其他人手动输入地址来保护管理页面。 我将此代码放在Web.config中。
//Web.config
<location path="Product">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
当管理员从具有部分登录页面的主页登录网站时,将通过数据库获取userName和admin是false还是true。
[HttpPost]
public ActionResult Index(Customer model)
{
if (ModelState.IsValid)
{
//define user whether admin or customer
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["rentalDB"].ToString());
String find_admin_query = "SELECT admin FROM Customer WHERE userName = '" + model.userName + "' AND admin ='true'";
SqlCommand cmd = new SqlCommand(find_admin_query, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
//it defines admin which is true or false
model.admin = sdr.HasRows;
conn.Close();
//if admin is logged in
if (model.admin == true) {
if (DAL.UserIsVaild(model.userName, model.password))
{
FormsAuthentication.SetAuthCookie(model.userName, true);
return RedirectToAction("Index", "Product");
}
}
//if customer is logged in
if (model.admin == false) {
if (DAL.UserIsVaild(model.userName, model.password))
{
FormsAuthentication.SetAuthCookie(model.userName, true);
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("", "The user name or password is incorrect.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
然后我的问题是,如何像使用model.userName或model.admin那样通过web.config而不是“ *”来定义用户? 您能告诉我如何定义用户吗? 谢谢。
根据您的问题,我不确定您要做什么。 听起来您有一个自定义的身份验证系统,但您仍要使用表单身份验证? 听起来有点混乱。 我不建议在同一站点上使用两个身份验证系统。 您可以编写一个自定义成员资格提供程序,但随后您将无法在web.config中定义用户。
为了回答问题的最后一部分,您可以在web.config中定义用户,如下所示:
<authentication mode="Forms">
<forms loginUrl="Logon.aspx" defaultUrl="Default.aspx">
<credentials passwordFormat="Clear">
<user name="user" password="pass" />
</credentials>
</forms>
</authentication>
要在MVC中使用上述用户,您需要将[Authorize]属性添加到控制器中,如下所示:
[Authorize]
public ActionResult Index(Customer model)
{
}
以上要求用户已经过身份验证。 否则,用户将被重定向到web.config中指定的loginUrl
。 不确定这是否会在您遇到的情况下起作用,因为您似乎希望所有用户都可以访问“索引”操作。
首先,您不能像使用ASP.NET WebForms一样使用web.config中的authorization
元素来保护路径。 这是因为MVC中的路由不是WebForms中的物理路径。
其次,您可能希望推出自己的MembershipProvider
和RoleProvider
,因为它将与ASP.NET和MVC很好地集成。 这非常琐碎,您可以替换自己的DAL来履行提供商合同。
实现自己的提供程序后,控制器的外观将如下所示:
public class AuthController : Controller
{
public ActionResult Index(Customer model)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.userName, model.password))
{
if (Roles.IsUserInRole(model.userName, "admin")) return RedirectToAction("Index", "Product");
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "The user name or password is incorrect.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
[Authorize(Roles = "user")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
[Authorize(Roles = "admin")]
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
}
如果您不想创建自己的提供程序,则还有两个其他选项来获得与[Authorization]
装饰相同的功能:
订阅您global.asax.cs中的AuthenticateRequest
事件,检查以确保User.Identity.IsAuthenticated
属性为true(此时,它将能够通过身份验证票证的形式告诉您) 。 如果是这样,请从DAL中加载您的角色并创建一个新的成员资格对象,并添加从DAL中找到的角色。 现在,您可以在其他任何地方使用AuthorizeAttribute
。
创建您自己的派生AuthorizeAttribute
,该派生AuthorizeAttribute
使用DAL获取用户角色。
您可能不想单独定义每个用户,而是使用角色。 然后,您可以使用Authorize属性或自定义的Authorization Filter中指定哪些角色可以执行哪些操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.