[英]How get POST request params in AuthorizationHandler
我正在ASP.NET Core 2.2中實現基於策略的授權,但是無法訪問Verification Handler中的POST請求參數。
我嘗試做這樣的事情:
mvcContext.HttpContext.Request.Form["key"]
但是當我訪問Request.Form給我這個錯誤:
'mvcContext.HttpContext.Request.Form' threw an exception of type 'System.InvalidOperationException'
我嘗試在Request.QueryString中使用GET參數並成功工作。
訪問POST參數的正確方法是什么? 我錯過了一些配置嗎?
你可以像這樣得到身體:
[HttpPost]
public string SampleMethod([FromBody] YourModel model)
{
//access to the model here
}
如果要訪問上下文,則應在啟動類中注冊IHttpContextAccessor
services.AddScoped<IHttpContextAccessor, HttpContextAccessor>();
根據您的屏幕截圖,似乎您想從請求json主體中讀取關鍵節點,如果是這樣,則可以嘗試將主體讀取為json,然后獲取節點值,例如
if (context.Resource is AuthorizationFilterContext mvcContext)
{
var request = mvcContext.HttpContext.Request;
request.EnableRewind();
var reader = new StreamReader(request.Body);
string body = reader.ReadToEnd();
var model = JsonConvert.DeserializeObject(body, mvcContext.ActionDescriptor.Parameters.FirstOrDefault().ParameterType);
JToken key;
JObject.Parse(body).TryGetValue("key", StringComparison.InvariantCultureIgnoreCase, out key);
request.Body.Seek(0, SeekOrigin.Begin);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.