我在ASP.NET Core 2.2中使用IdentityServer4。在Post Login方法上,我应用了ValidateAntiForgeryToken。通常,在坐在登录页面上20分钟到2个小时之后,尝试登录时会生成空白页面。
如果您查看Postman Console,则会收到400 Bad Request消息。然后,我将AntiForgery选项上的Cookie有效期设置为90天。我能够让该页面坐满6个小时,并且仍然可以登录。但是,大约8个小时(过夜)后,尝试登录后我又收到了空白页。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login
Run Code Online (Sandbox Code Playgroud)
services.AddAntiforgery(options =>
{
options.Cookie.Expiration = TimeSpan.FromDays(90);
});
Run Code Online (Sandbox Code Playgroud)
我希望能够在登录页面上停留90天,这是cookie的持续时间,但这是行不通的。如何获得AntiforgeryToken的Cookie可以持续整整90天或我设置的任何时间,而不是超时或过期?有没有一种方法可以捕获此错误并将用户重定向回登录方法?
AccountController一旦验证了用户身份,Identity Server 4 便会重定向到用于登录的HttpContext.SignInAsync方法,然后调用重定向到ReturnUrl的方法。
但是,在某些情况下internal server error,需要将其发送回原始客户端,而不是在视图中显示给最终用户。在这种情况下,我想发出标准的OAuth2错误响应,但是我看不到这样做的方法。
更新:
我添加了更多信息。我指的是OAuth 2.0规范的这一部分。Identity Server可以执行此操作,还是必须从RedirectUri手动构建URL。
基于此规范的RedirectUri的示例如下:
例如,授权服务器通过发送以下HTTP响应来重定向用户代理:
HTTP/1.1 302 Found
Location: https://client.example.com/cb?error=access_denied&state=xyz
Run Code Online (Sandbox Code Playgroud)
OAuth 2.0规范的第4.1.2.1节规定:
If the request fails due to a missing, invalid, or mismatching
redirection URI, or if the client identifier is missing or invalid,
the authorization server SHOULD inform the resource owner of the
error and MUST NOT automatically redirect the user-agent to the
invalid redirection URI.
If the resource owner denies the access request …Run Code Online (Sandbox Code Playgroud)