簡體   English   中英

如何根據傳遞的參數更改方法的Windows身份驗證?

[英]How can I change Windows auth for a method based on the parameters it is passed?

現在在我的MVC應用程序中,我正在使用Windows Auth來限制對某些方法的訪問,如下所示:

   [Authorize(Roles = SystemRole.AppAdmin)]

    public ActionResult Edit(int? id)
    {
        //Find group by id in db
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = SystemRole.AppAdmin)]
    public ActionResult Edit([Bind(Include = "id, otherParam")] Item thisItem)
        //Post changes to db
    }

我想擁有我的代碼,以便另一個廣告組具有有限的管理員權限,只能編輯具有特定ID的項目(例如,項目7),而不允許他們訪問此方法來嘗試編輯其他項目。

我已經嘗試使用谷歌搜索/搜索堆棧溢出/閱讀Windows Auth文檔,但是一定不能使用好的關鍵字,因為我當然不能成為第一個想要這樣做的人。

據我了解,您想要的是基於其AD組,允許組對特定組進行更改。

如果是這樣,請查看是否有一個包含所有這些管理員的AD組,並將其作為authorize屬性

[Authorize(Roles = SystemRole.SomeBaseAdminGroup)]

然后獲取他們的聲明,並根據返回特定方法或專用方法調用進行案例切換。 由於所有角色都被翻譯成“身份聲明”,

對於默認的 Windows身份驗證,這種復雜的授權有些棘手。

您將需要一種混合方法,其中您的應用程序將用戶及其授權存儲在單獨的數據庫中。

在此處輸入圖片說明

然后在登錄時將這些授權添加為聲明。 然后,您可以將Authorize屬性應用於操作方法和控制器。

public void SignIn(User user, IList<string> roleNames)
{
   IList<Claim> claims = new List<Claim>
   {
      new Claim(ClaimTypes.Sid, user.Id.ToString()),
      new Claim(ClaimTypes.Name, user.UserName),
      new Claim(ClaimTypes.GivenName, user.FirstName),
      new Claim(ClaimTypes.Surname, user.LastName),
   };

   foreach (string roleName in roleNames)
   {
      claims.Add(new Claim(ClaimTypes.Role, roleName));
   }

   ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

   IOwinContext context = _context.Request.GetOwinContext();
   IAuthenticationManager authenticationManager = context.Authentication;

   authenticationManager.SignIn(identity);
}

我在GitHub上有一個工作示例應用程序。 您可以看一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM