【发布时间】:2017-07-31 09:59:19
【问题描述】:
我正在开发一个示例 SPA,使用 ASP.NET MVC 作为后端,使用 Angular 2 作为前端。
我按照以下步骤在我的应用程序中防止跨站点请求伪造攻击
-
由于 ASP.NET MVC 发送一个名为“__RequestVerificationToken”的 cookie,并期望在 HTTP 请求中使用一个名为“__RequestVerificationToken”的标头来防止 CSRF,因此我在我的 Angular 模块中添加了以下代码
{provide: XSRFStrategy, useFactory: xsrfFactory}
xsrfFactory 在函数下面
export function xsrfFactory() {
return new CookieXSRFStrategy('__RequestVerificationToken', '__RequestVerificationToken');
}
-
下面是带有“[ValidateAntiForgeryToken]”属性的控制器动作代码,将使用Angular 2的Http服务对其进行AJAX调用。
[CustomAuth] [ValidateAntiForgeryToken] public ActionResult GetAuthors() { List<BookStoreAdmin.ViewModels.Author> authors = BookStoreAdmin.BAL.Author.GetAuthors(); BookStoreAdmin.ViewModels.Response<List<BookStoreAdmin.ViewModels.Author>> response = new Response<List<ViewModels.Author>>(); response.success = true; response.errorMessage = null; response.data = authors; return Json(response, JsonRequestBehavior.AllowGet); } -
下面是进行 AJAX 调用的代码。
loadAuthors(): Observable<AuthorModel[]> { return this.http.get('http://localhost:57599/author/GetAuthors') .map((data) => data.json()); }当我的应用程序使用 Http angular service 进行 AJAX 调用时,我希望它具有名称为 "__RequestVerificationToken" 的请求标头,但这 标题不见了,知道是什么原因吗?
如果需要提供更多信息,请告诉我?
【问题讨论】:
标签: asp.net-mvc angular csrf-protection