2011-07-23 193 views
1

我是ASP.NET新手,在ASP.NET上開發我的第一個基於web的應用程序。我應該做角色管理。項目中有三種文件夾; 1.ADMIN 2.MEMBER 3.ANOYNOMUS 我想在web.config端設置角色。如何在web.config中執行此操作? 我在網上找不到有用的信息,關於這個主題的數據庫方面? 我應該有一個表,大約是隻有角色嗎?還是我要補充「角色」屬性設置爲管理員和會員表..提前爲您的答覆 謝謝..一般ASP.NET角色管理

回答

1

This教程系列和this教程特別是創建和管理用戶角色應該對您有所幫助。

作爲開始,您還可以使用ASP.NET配置爲不同文件夾配置用戶角色和訪問規則。使用解決方案資源管理器(或Project \ Website-> ASP.NET配置)的右下角的小圖標打開它。

在ASP.NET配置中,使用安全性 - >訪問規則 - >管理相應文件夾的訪問規則。

0

----------------------------- AdminRoleController ---------------- --------------

using SamarpanInfotech.Areas.Admin.Controllers; 
using SamarpanInfotech.Core; 
using SamarpanInfotech.DataModel; 
using SamarpanInfotech.Services; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 

namespace SamarpanInfotech.Controllers 
{ 
    [RoutePrefix("Admin")] 
    public class AdminRoleController : AdminBaseController 
    { 
     RoleServices roleServices = new RoleServices(); 
     [Route("Role")] 
     public ActionResult Index() 
     { 
      var _list = roleServices.GetRole(1, 20, "RoleName descending", string.Empty); 
      return View(_list); 
     } 
     [Route("Role/Filter")] 
     public PartialViewResult FilterList(int page = 1, int pagerow = 20, string sortby = "RoleName descending", string search = null) 
     { 
      var _list = roleServices.GetRole(page, pagerow, sortby, search); 
      return PartialView("~/Areas/Admin/Views/AdminRole/DisplayTemplates/_List.cshtml", _list); 
     } 
     [Route("Role/Add")] 
     public ActionResult AddRole() 
     { 
      RoleRightListModel model = new RoleRightListModel(); 
      model.RightModel = roleServices.GetRightModel(); 
      return View(model); 
     } 
     [HttpPost] 
     [Route("Role/Add")] 
     public ActionResult AddRolePost(RoleRightListModel model) 
     { 
      roleServices.AddEditRoleRight(model, ProjectSession.AdminUser.Id); 
      this.ShowMessage(MessageExtension.MessageType.Success, "Record successfully saved", true); 
      return RedirectToAction("Index","AdminRole"); 
     } 
     [Route("Role/Edit/{Id}")] 
     public ActionResult EditRole(Guid? Id) 
     { 
      if (Id == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 
      var editRoleRight = roleServices.GetRoleRightModel(Id); 
      if (editRoleRight == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(editRoleRight); 
     } 
     [HttpPost] 
     [Route("Role/Edit/{Id}")] 
     public ActionResult EditRolePost(RoleRightListModel model) 
     { 
      roleServices.AddEditRoleRight(model); 
      this.ShowMessage(MessageExtension.MessageType.Success, "Successfully updated", true); 
      return RedirectToAction("Index", "AdminRole"); 
     } 
     [Route("Role/Delete/{Id}")] 
     public ActionResult DeleteRole(Guid Id) 
     { 
      roleServices.Delete(Id); 
      this.ShowMessage(MessageExtension.MessageType.Success, "Successfully deleted", true); 
      return RedirectToAction("Index", "AdminRole"); 
     } 
    } 
} 

----------------------------- ----- Index.cshtml -------------------------------

@model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.Role> 
@{ 
    ViewBag.Title = "Index"; 
} 
@section styles{ 
    <link href="~/assets/layouts/layout/css/custom.min.css" rel="stylesheet" /> 
} 
<!-- BEGIN PAGE BAR --> 
<div class="page-bar m-btm-20"> 
    <ul class="page-breadcrumb"> 
     <li> 
      <a href="@Url.Action("Index", "AdminDashboard")">Home</a> 
      <i class="fa fa-circle"></i> 
     </li> 
     <li> 
      <span>Role</span> 
     </li> 
    </ul> 
</div> 
<!-- END PAGE BAR --> 
<form method="post" class="form-horizontal"> 
    @Html.RenderMessages() 
    <div class="row"> 
     <div class="col-md-12"> 
      <div class="table-toolbar"> 
       <div class="row"> 
        <div class="col-md-6 col-sm-6"> 
         <div class="btn-group"> 
          <a class="btn green" href="@Url.Action("AddRole","AdminRole")"> 
           Add Role 
           <i class="fa fa-plus"></i> 
          </a> 
         </div> 
        </div> 
        <div class="col-md-4 col-sm-4 col-md-offset-2"> 
         <div class="input-group"> 
          <input type="text" placeholder="Searching..." name="search" class="form-control input-sm in-sm-h-35 input-inline input-lg input-lg-pd5" id="extendBox"> 
          <span class="input-group-btn"> 
           <button class="btn green in-sm-h-35" type="button" id="btnSearch" action-url="@Url.Action("FilterList", "AdminRole")" target-id="#target-load"> 
            Search 
           </button> 
          </span> 
         </div> 
        </div> 
       </div> 
      </div> 
      <div class="target-load hide-x" id="target-load"> 
       @Html.Partial("~/Areas/Admin/Views/AdminRole/DisplayTemplates/_List.cshtml", Model) 
      </div> 
     </div> 
    </div> 
</form> 
@section scripts 
{ 
    <script> 
     $(document).on('click', ".delete", function (e) { 
      if (confirm("Are you sure want to delete?")) { 
       return true; 
      } 
      e.preventDefault(); 
      return false; 
     }); 
    </script> 
} 

---- --------------------------_ List.cshtml(Partial View)----------------- ----

@model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.Role> 
<table class="table table-bordered dataTable" target=".target-load" id="sample_editable_1"> 
    <thead> 
     <tr> 
      @Html.Sorting(Url.Action("FilterList"), "RoleName", "Role Name", Model.SortBy, new { item_page = Model.PageRows }) 
      @Html.Sorting(Url.Action("FilterList"), "AboutRole", "About Role", Model.SortBy, new { item_page = Model.PageRows }) 
      <th>Action</th> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach (var item in Model.PageList) 
     { 
      <tr class="gradeX"> 
       <td>@item.RoleName</td> 
       <td>@item.AboutRole</td> 
       <td> 
        <a href="@Url.Action("EditRole", "AdminRole", new { Id = item.Id })" title="Edit" class="btn btn-outline btn-circle btn-sm purple"> 
         <i class="fa fa-edit"></i> 
        </a> 
        <a href="@Url.Action("DeleteRole", "AdminRole", new { Id = item.Id })" title="Delete" class="btn btn-outline btn-circle btn-sm red delete"> 
         <i class="fa fa-trash-o"></i> 
        </a> 
       </td> 
      </tr> 
     } 

    </tbody> 
</table> 
<div class="row master-pager"> 
    @Html.PagerFrontend(Url.Action("FilterList", "AdminRole"), Model.PageNumber, Model.TotalRows, Model.PageRows, Model.SortBy) 
</div> 

---- ---------------------------- AddRole.cshtml ------------------- ---------

@model SamarpanInfotech.DataModel.RoleRightListModel 
@{ 
    ViewBag.Title = "AddRole"; 
} 
<div class="row"> 
    <div class="col-md-12"> 
     <div class="portlet light portlet-fit portlet-form bordered"> 
      <div class="portlet-title"> 
       <div class="caption"> 
        <span>Create Role</span> 
       </div> 
      </div> 
      @using (Html.BeginForm("AddRolePost", "AdminRole", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
      { 
       <div class="portlet-body form"> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.RoleCode, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextBoxFor(model => model.RoleCode, new { @class = "form-control", placeholder = "Role Code" }) 
          @Html.ValidationMessageFor(model => model.RoleCode, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.RoleName, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextBoxFor(model => model.RoleName, new { @class = "form-control", placeholder = "Role Name" }) 
          @Html.ValidationMessageFor(model => model.RoleName, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.AboutRole, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextAreaFor(model => model.AboutRole, new { @class = "form-control", placeholder = "About Role" }) 
          @Html.ValidationMessageFor(model => model.AboutRole, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
       </div> 
       <table class="table table-bordered table-striped dataTable"> 
        <thead> 
         <tr> 
          <th>Rights</th> 
          <th>IsAll ?</th> 
          <th>IsView ?</th> 
          <th>IsAdd ?</th> 
          <th>IsEdit ?</th> 
          <th>IsDelete ?</th> 
         </tr> 
        </thead> 
        <tbody> 
         @{ 
       var count = 0; 
         } 
         @foreach (var item in Model.RightModel) 
         { 
          <tr class="gradeX"> 

           @Html.Hidden("RightModel[" + count + "].RightId", item.RightId, new { @RightId = "RightModel[" + count + "].RightId" }) 
           <td>@item.RightName</td> 
           <td>@Html.RightCheckBox("IsAllVisible", item.IsAllRight, "RightModel[" + count + "]", item.IsAllVisible)</td> 
           <td>@Html.RightCheckBox("IsViewVisible", item.IsViewRight, "RightModel[" + count + "]", item.IsViewVisible)</td> 
           <td>@Html.RightCheckBox("IsAddVisible", item.IsAddRight, "RightModel[" + count + "]", item.IsAddVisible)</td> 
           <td>@Html.RightCheckBox("IsEditVisible", item.IsEditRight, "RightModel[" + count + "]", item.IsEditVisible)</td> 
           <td>@Html.RightCheckBox("IsDeleteVisible", item.IsDeleteRight, "RightModel[" + count + "]", item.IsDeleteVisible)</td> 
          </tr> 
          count = count + 1; 
         } 

        </tbody> 
       </table> 
       <div class="row m-btm-20"> 
        <div class="col-md-offset-2 col-md-9"> 
         <input type="submit" class="btn blue" value="Submit"> 
         <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminRole")">Cancel</a> 
        </div> 
       </div> 
      } 
     </div> 
    </div> 
</div> 

-------------------------------- EditRole。 cshtml ------------------------------

@model SamarpanInfotech.DataModel.RoleRightListModel 
@{ 
    ViewBag.Title = "EditRole"; 
} 
<div class="row"> 
    <div class="col-md-12"> 
     <div class="portlet light portlet-fit portlet-form bordered"> 
      <div class="portlet-title"> 
        <div class="caption"> 
         <span>Edit Role</span> 
        </div> 
       </div> 
      @using (Html.BeginForm("EditRolePost", "AdminRole", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
      { 
       @Html.HiddenFor(model => model.RoleId) 
       <div class="portlet-body form"> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.RoleCode, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextBoxFor(model => model.RoleCode, new { @class = "form-control", placeholder = "Role Code" }) 
          @Html.ValidationMessageFor(model => model.RoleCode, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.RoleName, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextBoxFor(model => model.RoleName, new { @class = "form-control", placeholder = "Role Name" }) 
          @Html.ValidationMessageFor(model => model.RoleName, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
        <div class="form-group"> 
         @Html.LabelFor(m => m.AboutRole, new { @class = "control-label col-md-2" }) 
         <div class="col-md-3"> 
          @Html.TextAreaFor(model => model.AboutRole, new { @class = "form-control", placeholder = "About Role" }) 
          @Html.ValidationMessageFor(model => model.AboutRole, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
       </div> 
       <table class="table table-bordered table-striped dataTable"> 
        <thead> 
         <tr> 
          <th>Rights</th> 
          <th>IsAll ?</th> 
          <th>IsView ?</th> 
          <th>IsAdd ?</th> 
          <th>IsEdit ?</th> 
          <th>IsDelete ?</th> 
         </tr> 
        </thead> 
        <tbody> 
         @{ 
          var count = 0; 
         } 
         @foreach (var item in Model.RightModel) 
         { 
          <tr class="gradeX"> 
           @Html.Hidden("RightModel[" + count + "].RightId", item.RightId, new { @RightId = "RightModel[" + count + "].RightId" }) 
           <td>@item.RightName</td> 
           <td>@Html.RightCheckBox("IsAllVisible", item.IsAllRight, "RightModel[" + count + "]", item.IsAllVisible)</td> 
           <td>@Html.RightCheckBox("IsViewVisible", item.IsViewRight, "RightModel[" + count + "]", item.IsViewVisible)</td> 
           <td>@Html.RightCheckBox("IsAddVisible", item.IsAddRight, "RightModel[" + count + "]", item.IsAddVisible)</td> 
           <td>@Html.RightCheckBox("IsEditVisible", item.IsEditRight, "RightModel[" + count + "]", item.IsEditVisible)</td> 
           <td>@Html.RightCheckBox("IsDeleteVisible", item.IsDeleteRight, "RightModel[" + count + "]", item.IsDeleteVisible)</td> 
          </tr> 
          count = count + 1; 
         } 

        </tbody> 
       </table> 
       <div class="row m-btm-20"> 
        <div class="col-md-offset-2 col-md-9"> 
         <input type="submit" class="btn blue" value="Submit"> 
         <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminRole")">Cancel</a> 
        </div> 
       </div> 
      } 
     </div> 
    </div> 
</div> 
<script> 
    $(document).ready(function() { 
     $(document).on("change", "#IsAllVisible", function (e) { 
      var CheckIsAll = $('#IsAllVisible').val(); 
      if (CheckIsAll == true) 
      { 
       $('#IsViewVisible').val() == true; 
       $('#IsAddVisible').val() == true; 
       $('#IsEditVisible').val() == true; 
       $('#IsDeleteVisible').val() == true; 
      } 
     }) 
    }); 
</script>