2012-03-10 30 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屬性或在您的自定義授權篩選器中指定哪些角色可以執行哪項操作。