プロジェクトのために、私はmvc3 asp.netアプリケーションを作っています。私が実装したいと思う機能の1つは、緩やかに結合されたRole - Action属性です。基本的には、管理ユーザーはロールを作成してアクションにリンクする必要があります。Asp.net 4 MVC3の疎結合の役割とアクション
ロール自体がデータベースに格納されます。
私は、次のアプローチの行に何かがうまくいくかどうかを疑問に思って:
//Definition of a the Attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyFlexibleAttribute : AuthorizeAttribute
{
private Repository RolesRepository = new Repository();
public String ActionName {get; set;}
public MyFlexibleAttribute()
{
Roles = RolesRepository.getStringRolesSeparatedByComma(ActionName)
}
}
// where in a random controller of a view, I could state
[MyFlexibleAttribute(ActionName = "SpecialAction"]
public ActionResult SpecialAction()
{
return View();
}
あなたの意見は何ですか? DBのコンテキストを使用している場合
おかげ
* UPDATE ** こんにちは、私は最近、上記の設計といくつかの可能性lacunasを発見しました。カスタム属性の有効期間がそれ以上であれば、属性のインスタンス化時に新しいリポジトリを呼び出すコントローラの寿命は、リポジトリの内部状態による可能性のある不均衡やセキュリティリークを引き起こす可能性があります(これがインスタンスdbContext )。
E.G.
public ActionTypeName ActionTypeName {get; set;}
private Repository repo;
public CustomAuthorizeAction(ActionTypeName actionTypeName)
{
this.ActionTypeName = actionTypeName;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
//instantiate new instance when calling the OnAuthorization method
this.repo = new Repository();
List<Role> tmp = repo.getRolesLinkedToAction(this.ActionTypeName).ToList();
Roles = String.Join(",", tmp.Select(r => r.Name));
base.OnAuthorization(filterContext);
}
希望これは、それは、はい
ありがとう、すべて私は知る必要があった – cecemel
こんにちは、私は最近、DBコンテキストを使用する場合、上記の設計でいくつかの可能性のあるラクナを発見した。カスタム属性の有効期間がそれ以上であれば、属性のインスタンス化時に新しいリポジトリを呼び出すコントローラの寿命は、リポジトリの内部状態による可能性のある不均衡やセキュリティリークを引き起こす可能性があります(これがインスタンスdbContext ) – cecemel