2010-11-30 86 views
1

我正在修改用C#MVC編寫的系統。授權屬性不使用角色MVC C#

我剛剛在管理員區域內建立了額外的功能,允許管理員創建具有有限管理員功能的用戶帳戶。我已經把以下在每個控制器的新功能:

[Authorize(Roles = "Administrator")] 

不過,如果我登錄使用受限管理員帳戶,並導航到這個頁面,它讓我通過。

我很難過,因爲我似乎正在做這個正確的方式,但我也是相當新的MVC,有什麼我可以檢查?我沒有改變任何web.config文件,所以應該沒問題。

我知道上面的信息有限,沒有尋找現成的解決方案,更多關於我可以檢查以糾正問題的建議。

感謝

編輯:

這是新的角色/賬號是如何產生的。也很容易,這是第一次嘗試,沒有太多驗證。

[Authorize(Roles = "Administrator")] 
    [HttpPost] 
    public ActionResult AddSalesManager(App.Web.Areas.Administrator.Models.SalesManager model, FormCollection formValues) 
    { 
     if (formValues["Cancel"] != null) 
     { 
      return RedirectToAction("Index"); 
     } 

     if (!string.Equals(model.password, model.confirmpassword)) 
     { 
      ModelState.AddModelError("password", "Password and Confirmation must match"); 
     } 

     if (ModelState.IsValid) 
     { 
      using (ModelContainer ctn = new ModelContainer()) 
      { 
       // First, create the user account inside the ASP.Net membership system. 
       // 

       Membership.ApplicationName = "App"; 
       Roles.ApplicationName = "App"; 

       if (!Roles.RoleExists("LimitedAdmin")) 
        Roles.CreateRole("LimitedAdmin"); 

       // MembershipCreateStatus createStatus = MembershipService.CreateUser(model.email, model.password, model.email); 
       if (Membership.GetUser(model.email) == null) 
       { 
        Membership.CreateUser(model.email, model.password); 
        Roles.AddUserToRole(model.email, "LimitedAdmin"); 
       } 

      } 
     } 
     return RedirectToAction("Index"); 

    } 
+1

發佈你如何設置的角色會有所幫助。 – jfar 2010-11-30 16:00:35

+0

會做,兩秒鐘。 – 109221793 2010-11-30 16:06:05

回答

2

您對此代碼有什麼期待?

使用此屬性,您將獲得管理員角色中的所有用戶執行此控制器操作的權限,無論該帳戶的限制如何。

3

角色屬性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public class PermissionsAttribute : ActionFilterAttribute 
{ 
    private readonly PermissionsType required; 

    public PermissionsAttribute() 
    { 
    } 

    public PermissionsAttribute(PermissionsType required) 
    { 
     this.required = required; 
    } 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     // Log("OnActionExecuting", filterContext.RouteData); 

     HttpSessionStateBase session = filterContext.HttpContext.Session; 
     Controller controller = filterContext.Controller as Controller; 

     //This is uesd to redirect to same controller but differnect action 
     // controller.HttpContext.Response.Redirect("./Login"); 

     var rjasthan = filterContext; 


     var URK = filterContext.HttpContext.Request.RawUrl; 
     if (session["UserPermissions"] != null) 
     { 
      if (!CheckPermissions((UserPermission)session["UserPermissions"])) 
      { 
       // this is used to signout from sesssion 
       // filterContext.HttpContext.GetOwinContext().Authentication.SignOut(); 

       filterContext.Controller.TempData["AuthenticationMessages"] = "You are not authorized to access"; 

       filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary{ 
          { "controller", "Home" },{ "action", "UnAuthorizeAccess" }}); 

      } 
     } 

     base.OnActionExecuting(filterContext); 

    } 

    protected bool CheckPermissions(UserPermission model) 
    { 
     bool result = false; 

     if (this.required == (PermissionsType.Add)) 
     { 
      if (model.AddRight) 
       result = true; 
     } 
     else if (this.required == (PermissionsType.View)) 
     { 
      if (model.ViewRight) 
       result = true; 
     } 
     else if (this.required == (PermissionsType.Edit)) 
     { 
      if (model.EditRight) 
       result = true; 
     } 
     else if (this.required == (PermissionsType.Delete)) 
     { 
      if (model.DeleteRight) 
       result = true; 
     } 
     else if (this.required == (PermissionsType.View | PermissionsType.Edit)) 
     { 
      if (model.ViewRight && model.EditRight) 
      { 
       result = true; 
      } 
     } 
     else if (this.required == (PermissionsType.Add | PermissionsType.Edit)) 
     { 
      if (model.AddRight && model.EditRight) 
      { 
       result = true; 
      } 
     } 


     return result; 
    } 

    private void Log(string methodName, RouteData routeData) 
    { 
     var controllerName = routeData.Values["controller"]; 
     var actionName = routeData.Values["action"]; 
     var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName); 
     Debug.WriteLine(message, "Action Filter Log"); 
    } 
} 

[Flags] 
public enum PermissionsType 
{ 
    View = (1 << 0), 
    Add = (1 << 1), 
    Edit = (1 << 2), 
    Delete = (1 << 3), 
    Admin = (View | Add | Edit | Delete) 
} 



[Permissions(PermissionsType.Add)] 
    public ActionResult Register() 
    { 
     return this.AjaxableView(); 
    }