1

アクション メソッドに基づいてセキュリティ ロールを取得するための承認要件がありますが、これは既定の asp.net mvc 承認を使用して達成することはできません。そのため、カスタム認証要件を実装するために、次のアクション フィルターを作成しました。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class CheckUserPermissionsAttribute : ActionFilterAttribute
    {
        Repository repository = new Repository();
        public string Model { get; set; }
        public string Action { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
           // var user = User.Identity.Name; // or get from DB 
            string ADusername = filterContext.HttpContext.User.Identity.Name.Substring(filterContext.HttpContext.User.Identity.Name.IndexOf("\\") + 1);
            if (!repository.can(ADusername,Model,Action)) // implement this method based on your tables and logic
            {
                filterContext.Result = new HttpUnauthorizedResult("You cannot access this page");

            }

            base.OnActionExecuting(filterContext);
        }
    }

次のリポジトリメソッドを呼び出しています:-

public bool can(string user, string Model, string Action)
        {
            bool result;
            bool result2;


int size = tms.PermisionLevels.Where(a5 => a5.Name == Action).SingleOrDefault().PermisionSize;
var securityrole = tms.SecurityroleTypePermisions.Where(a => a.PermisionLevel.PermisionSize >= size && a.TechnologyType.Name == Model).Select(a => a.SecurityRole).Include(w=>w.Groups).Include(w2=>w2.SecurityRoleUsers).ToList();//.Any(a=> a.SecurityRoleUsers.Where(a2=>a2.UserName.ToLower() == user.ToLower()));

foreach (var item in securityrole)
                    {
result = item.SecurityRoleUsers.Any(a => a.UserName.ToLower() == user.ToLower());
var no = item.Groups.Select(a=>a.TMSUserGroups.Where(a2=>a2.UserName.ToLower() == user.ToLower()));
result2 = no.Count() == 1;
if (result || result2) 
{
    return true;
}}
return false;

次のように、コントローラークラス内でアクションフィルターを呼び出しています:-

   [CheckUserPermissions(Action = "Read", Model = "Server")]

しかし、私は次の懸念があります:-

  1. 私のリポジトリ内で、すべてのユーザーとグループを取得し(.Tolist()を呼び出すとき)、現在のログインユーザーがこれらの値内にあるかどうかを確認します。膨大な数のユーザーを扱う場合、どれが非常に拡張可能ではないでしょうか?

  2. ユーザーがアクション メソッドを呼び出すたびに、同じセキュリティ コードが実行されます (もちろん、理想的には、ユーザー セッション中にユーザーのアクセス許可が変更される可能性があります)。これにより、パフォーマンスの問題が発生する可能性があります。

では、2つの懸念事項を念頭に置いて、現在の実装を改善する方法を教えてもらえますか? ありがとう

4

1 に答える 1