NMa*_*hur 3 c# asp.net asp.net-mvc asp.net-web-api2
我需要在授权时获取我的 Post 参数的值。网络上的搜索者,但没有解决方案。ActionArguments计数总是显示 0 并且无法找到值ActionDescriptor.GetParameters()
这是我的代码:
POST 模型 -
public class XyzModel
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
自定义授权属性 -
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
bool conditions = // here I need to check value of my model (XyzModel) properties
if(conditions)
{
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
控制器中的代码 -
[HttpPost]
[CustomAuthorizeAttribute]
public IHttpActionResult MyAction(XyzModel model)
{
// my work here
}
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
您可以访问 ActionArguments 的模型属性,它将返回 XyzModel 对象。您可以对其属性执行任何操作:
XyzModel model = (XyzModel)actionContext.ActionArguments["model"];
Run Code Online (Sandbox Code Playgroud)
在您的代码中,它将是这样的:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var prop1 = HttpContext.Current.Request.Params["Prop1"];
var prop2 = HttpContext.Current.Request.Params["Prop2"];
bool conditions = // add conditions based on above properties
if(conditions)
{
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4391 次 |
| 最近记录: |