2012-03-10 22 views
0

我想通过使用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?你能告诉我如何定义用户吗?谢谢。

回答

2

首先,您不能在web.config中使用authorization元素来像ASP.NET WebForms一样保护路径。这是因为MVC中的路由不像WebForms中的物理路径。其次,你可能希望推出自己的MembershipProviderRoleProvider,因为它将很好地与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]装饰品:

  1. 订阅在Global.asax中AuthenticateRequest事件。cs,请检查以确保User.Identity.IsAuthenticated属性为真(它将能够通过表单验证票证将在此处为您处理)。如果是,则从DAL加载您的角色并创建一个新的成员资格对象,并添加从DAL中找到的角色。现在你可以在其他地方使用AuthorizeAttribute

  2. 创建您自己的衍生产品AuthorizeAttribute,它使用您的DAL获取用户的角色。

+0

谢谢你的回复,当我把Roles.IsUserInRole(model.userName,“admin”),它不起作用[Authorize(Roles =“admin”)]。如果我需要创建AuthorizeAttribute来赋予特定角色,我该如何做到这一点?我是一名MVC初学者,那么你能解释一下如何实现它吗?当我研究它时,我不知道该怎么做。 – wholee1 2012-03-11 20:57:47

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] 
public ActionResult Index(Customer model) 
{ 
} 

以上要求用户已经进行了身份验证。如果不是,用户将被重定向到web.config中指定的loginUrl。不确定这会在你的情况下工作,因为它听起来像你希望所有用户访问您的索引行动。

+0

其实我定义了两种类型的用户;管理员和客户通过位型的管理员字段。在我的情况下,如果model.admin的客户为false,登录后进入管理端的Product页面,即使用户是客户,也可以访问Product页面。然后我想用model.admin处理是true或false。我可以在web.config或其他页面中处理model.admin吗? – wholee1 2012-03-11 00:07:52

0

您可能不想单独定义每个用户,而是使用角色。然后,您可以通过使用Authorize属性或在您的自定义授权筛选器中指定哪些角色可以执行哪项操作。