【发布时间】:2017-05-20 08:42:37
【问题描述】:
我正在使用 IdentityServer 3 进行身份验证。当用户通过身份验证时,IdentityServer 向客户端的应用程序 URL 发出“POST”请求。例如 http://localhost/home 到目前为止一切正常。
我的客户端应用程序是在 ASP.NET Core 中开发的。在客户端应用程序中,我想验证每个 POST 请求。因此,我没有在每个操作方法上添加 ValidateAntiForgeryToken 属性,而是创建了一个中间件来验证每个 POST 请求。
public class ValidateAntiForgeryTokenMiddleware
{
private readonly RequestDelegate _next;
private readonly IAntiforgery _antiforgery;
public ValidateAntiForgeryTokenMiddleware(RequestDelegate next, IAntiforgery antiforgery)
{
_next = next;
_antiforgery = antiforgery;
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Method.ToUpper() == "POST")
{
await _antiforgery.ValidateRequestAsync(httpContext);
}
await _next(httpContext);
}
}
这里的问题是由于身份服务器也在进行 POST,中间件尝试验证该请求,但 POST 请求失败并出现以下错误
"所需的防伪cookie \".AspNetCore.Antiforgery.AXelvXewLHI\" 不存在。"
在 Identity Server 中,我有自定义登录页面。我已经在登录页面配置了防伪令牌。
<div ng-show="model.loginUrl">
<div class="cr-login-dialog col-md-6 col-md-offset-3">
<form name="form" method="post" action="{{model.loginUrl}}" class="form-horizontal" role="form">
<anti-forgery-token token="model.antiForgery"></anti-forgery-token>
<div class="form-group">
//user name controls goes here
</div>
<div class="form-group">
//password controls goes here
</div>
<div class="form-group" ng-show="model.allowRememberMe">
// remember me controls goes here
</div>
</form>
</div>
</div>
【问题讨论】:
标签: asp.net-core-mvc identityserver3 antiforgerytoken