我成功地使用CORS设置AWS API Gateway,当请求有效时,我有200状态代码和CORS头,这很好.
但是当AWS API网关请求验证程序检测到无效输入时,我的状态代码为400但不发送CORS标头...
这真的很不幸,因为客户端看到了CORS错误而不是真正的400错误(例如fetch客户端由于CORS错误而无法区分400错误).
此问题可能与从AWS API网关请求验证程序获取详细错误消息有关
因此,我正在使用以下代码来验证表单的输入:
$request->validate([
'title' => 'bail|required|max:255',
'body' => 'required',
]);
Run Code Online (Sandbox Code Playgroud)
基本上,表单中有两个字段,标题和正文,它们具有上述规则。现在,如果验证失败,我想直接在控制器中捕获错误,然后再重定向到视图,以便我可以发送错误消息作为 Post 请求的响应。最好的方法是什么?我知道错误被推送到会话中,但这是由视图来处理的,但我想在控制器本身中处理此类错误。
谢谢
我有这个签名的控制器方法:
public async IAsyncEnumerable<MyDto> Get()
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我需要做一些请求验证并相应地返回 401、400 和其他代码,但它不支持。或者,以下签名不会编译:
public async Task<ActionResult<IAsyncEnumerable<MyDto>>> Get()
Run Code Online (Sandbox Code Playgroud)
错误:
无法将类型“Microsoft.AspNetCore.Mvc.UnauthorizedResult”隐式转换为“MyApi.Responses.MyDto”
完整方法:
public async IAsyncEnumerable<MyDto> Get()
{
if (IsRequestInvalid())
{
// Can't do the following. Does not compile.
yield return Unauthorized();
}
var retrievedDtos = _someService.GetAllDtosAsync(_userId);
await foreach (var currentDto in retrievedDtos)
{
yield return currentDto;
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?似乎无法相信微软的设计目的IAsyncEnumerable是在没有返回任何其他东西的可能性/灵活性的情况下使用。
c# actionresult request-validation asp.net-core-webapi iasyncenumerable
假设我有以下自定义请求:
class PlanRequest extends FormRequest
{
// ...
public function rules()
{
return
[
'name' => 'required|string|min:3|max:191',
'monthly_fee' => 'required|numeric|min:0',
'transaction_fee' => 'required|numeric|min:0',
'processing_fee' => 'required|numeric|min:0|max:100',
'annual_fee' => 'required|numeric|min:0',
'setup_fee' => 'required|numeric|min:0',
'organization_id' => 'exists:organizations,id',
];
}
}
Run Code Online (Sandbox Code Playgroud)
当我从控制器访问它时,如果我这样做$request->all(),它会给我所有数据,包括不想传递的额外垃圾数据.
public function store(PlanRequest $request)
{
dd($request->all());
// This returns
[
'name' => 'value',
'monthly_fee' => '1.23',
'transaction_fee' => '1.23',
'processing_fee' => '1.23',
'annual_fee' => '1.23',
'setup_fee' => '1.23',
'organization_id' => null,
'foo' => 'bar', // This is …Run Code Online (Sandbox Code Playgroud) 美好的一天!
我打算将我的ASP.NET MVC 2应用程序升级到.NET 4.0,并且有几个问题:
正在[ValidateInput(false)]采取行动足以接受HTML,或者我需要<httpRuntime requestValidationMode="2.0"/>按照此处所述进行设置:ASP.NET 4 Breaking Changes
如果我将ASP.NET MVC升级到版本3(除了升级到.NET 4.0)之外它将如何工作?
提前致谢!
我们使用Jasig .NET CAS Client与我们组织的CAS SSO服务器连接.
但是,我们注意到在ASP.NET MVC 3中(我认为这会影响ASP.NET WebForms)应用程序,当用户注销时,我们在错误日志中看到以下错误:
System.Web.HttpRequestValidationException (0x80004005):
A potentially dangerous Request.Form value was detected from the client
(logoutRequest="<samlp:LogoutRequest...").
at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection)
at System.Web.HttpRequest.get_Form()
at System.Web.HttpRequest.FillInParamsCollection()
at System.Web.HttpRequest.GetParams()
at DotNetCasClient.Utils.RequestEvaluator.GetRequestIsCasSingleSignOut() in C:\Projects\Jasig\CAS\dotnet-client\trunk\DotNetCasClient\Utils\RequestEvaluator.cs:line 292
at DotNetCasClient.CasAuthenticationModule.OnBeginRequest(Object sender, EventArgs e) in C:\Projects\Jasig\CAS\dotnet-client\trunk\DotNetCasClient\CasAuthenticationModule.cs:line 93
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Run Code Online (Sandbox Code Playgroud)
我不相信这是用户收到的错误消息 - 它似乎只能被服务器看到.就用户而言,注销成功.
有没有办法让ASP.NET MVC停止尝试验证这些类型的请求?我知道我可以完全禁用请求验证,但这是不可能的.带有连字符的网站对此有一个很好的问题,但不是一个真正可以接受的答案:
将以下设置添加到web.config:
<httpRuntime requestValidationMode="2.0" />设置此值后,U可以通过设置validateRequest ="false"来禁用请求验证
那么,有没有办法为这个请求禁用ASP.NET验证而不完全关闭它?
编辑:调试也很棘手,因为此请求来自CAS服务器,而不是来自用户的浏览器.我认为这是CAS服务器试图通知用户已注销的所有正在运行的应用程序(单点注销 …
我正在使用ASP.NET WebForms(.NET 4.5),并具有“内容块”控件,该控件可在许多页面上重复使用。我尝试将控件甚至单个元素的ValidateRequestMode设置为“ Disabled”,但来自web.config的请求验证仍可防止输入不安全。
有办法解决还是我做错了什么?
例:
语境
我注意到,在 VS 2019 中从开箱即用的模板创建新的 ASP.NET Core Razor 页面应用程序后,即使是具有最纯粹模型类的最纯粹的html 表单也会使用<input name="__RequestVerificationToken" type="hidden" value="...">
问题
我是否遗漏了某些内容,并且在某个地方有一个显式属性/语句指示 ASP.NET Core 添加防伪功能,或者现在这是默认设置?(这使得 using[AutoValidateAntiforgeryToken]过时了)
...或者...
这只是始终无条件<input name="__RequestVerificationToken" type="hidden" value="...">渲染的,并且我可以针对它打开服务器端验证吗?这种情况下如何进行冒烟测试验证是否有效?[AutoValidateAntiforgeryToken]
示例代码
@page
@model TestFormModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<form method="post">
<input type="text" name="myinput"/>
<input type="submit" value="Submit" />
</form>
</div>
//[AutoValidateAntiforgeryToken]
public class TestFormModel : PageModel
{
private readonly ILogger<TestFormModel> _logger;
public TestFormModel(ILogger<TestFormModel> logger)
{
_logger = logger;
}
public void OnGet()
{ …Run Code Online (Sandbox Code Playgroud) request-validation antiforgerytoken asp.net-core razor-pages
asp.net ×3
c# ×2
laravel ×2
php ×2
validation ×2
.net ×1
actionresult ×1
asp.net-core ×1
asp.net-mvc ×1
cas ×1
cors ×1
iis-7 ×1
razor-pages ×1
rest ×1
webforms ×1