我正在尝试使用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);
}
然后我的问题是,我如何通过web.config而不是“*”来定义用户,比如使用model.userName或model.admin?你能告诉我如何定义用户吗?感谢。
答案 0 :(得分:2)
从你的问题我不完全确定你想做什么。听起来你有一个自定义身份验证系统,但你仍然想使用表单身份验证?听起来有点混乱。我不建议在同一站点上使用两个身份验证系统。您可以编写自定义成员资格提供程序,但之后不会在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
。不确定这会在您的情况下有效,因为您似乎希望所有用户都能访问您的索引操作。
答案 1 :(得分:2)
首先,您不能使用web.config中的authorization
元素来保护路径,就像保护ASP.NET WebForms一样。这是因为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
,使用您的DAL来获取用户的角色。
答案 2 :(得分:0)
您可能不希望单独定义每个用户,而是使用角色。然后,您可以使用Authorize属性或自定义授权过滤器指定哪些角色可以执行哪些操作。