我想通过使用web.config进行授权。 在我的用户注册中,它没有使用ASP.NET配置。 我正在处理数据库的登录页面。 我想保护管理页面为手动输入其他人的地址。 我把这段代码放在Web.config中。MVC 3位置允许web.config中的用户
//Web.config
<location path="Product">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
当管理员登录网站由首页其中有部分登录页面, 它会获取用户名和管理员是否通过数据库或真或假。
[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);
}
然后我的问题是,我怎么能由web.config中,而不是“*”定义用户,喜欢用model.userName或model.admin?你能告诉我如何定义用户吗?谢谢。
谢谢你的回复,当我把Roles.IsUserInRole(model.userName,“admin”),它不起作用[Authorize(Roles =“admin”)]。如果我需要创建AuthorizeAttribute来赋予特定角色,我该如何做到这一点?我是一名MVC初学者,那么你能解释一下如何实现它吗?当我研究它时,我不知道该怎么做。 – wholee1 2012-03-11 20:57:47