ASP MVC C#: Is it possible to pass dynamic values into an attribute?

前端 未结 3 410
鱼传尺愫
鱼传尺愫 2021-01-03 00:32

Okay I\'m very new to C# and i\'m trying to create a little website using ASP MVC2.

I want to create my own authorization attribute. but i need to pass some values i

相关标签:
3条回答
  • 2021-01-03 01:18

    You get it from Request.Form

    public class CustomAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { //httpContext.Request.Form["groupid"] return base.AuthorizeCore(httpContext); } }

    0 讨论(0)
  • 2021-01-03 01:20

    You get it from Request.Form

    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
           //httpContext.Request.Form["groupid"]
            return base.AuthorizeCore(httpContext);
        }
    }
    
    0 讨论(0)
  • 2021-01-03 01:21

    Use the value provider:

    public class CustomAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var result = filterContext.Controller.ValueProvider.GetValue("GroupId"); //groupId should be of type `ValueProviderResult`
    
            if (result != null)
            {
                int groupId = int.Parse(result.AttemptedValue);
    
                //Authorize the user using the groupId   
            }
       }
    

    }

    This article may help you.

    HTHs,
    Charles

    0 讨论(0)
提交回复
热议问题