HttpParameterBinding を使用して、webAPI アクションのパラメーターに値を割り当てるだけでなく、アクセス許可を確認するためにも使用しています。
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
{
//I get the userId from another parameter that is set from another parameter binding
int userId = Convert.ToInt32(actionContext.ActionArguments["userId"]);
var permission = getPermissions(userId);
if (permission < minRight)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Permission Denied.")
};
}
actionContext.ActionArguments[Descriptor.ParameterName] = permission;
var taskSource = new TaskCompletionSource<object>();
taskSource.SetResult(null);
return taskSource.Task;
}
ただし、これは実行を続けます。できれば例外をスローせずに、ここで 401 応答を返し、実行を継続しない方法はありますか。