爲了防止跨邊界腳本,我實現了CSP到我的一個應用程序。在這一刻我重新配置所有的html類,以便JavaScript始終來自我的服務器。MVC Ajax.BeginForm和內容安全策略
現在我發現了一個頁面,其中包含一個Ajax.BeginForm
,如果我想提交表單並更新視圖,則始終會顯示錯誤「Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self'".
」。
任何人都可以幫助我,問題在哪裏?
這裏是我的HTML類(短路):
UserInformation.cshtml:
<div id="OpenAccountInformation">@Html.Action("OpenAccountInformation")</div>
</div>
AccountInformation.cshtml:
@Scripts.Render("~/Scripts/bundles/ManageUsers/AccountInformation")
@model Tumormodelle.Models.ViewModels.AzaraUserModel
<input type="hidden" value="@ViewBag.Editable" id="EditableUserInformation">
<div id="Editable">
@using (Ajax.BeginForm("EditUser", "ManageUsers", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "OpenAccountInformation", HttpMethod = "post", }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.UserID)
<div>
<div>
@Html.LabelFor(m => m.Username, new { @class = "entryFieldLabel" })
</div>
</div>
<div>
<div>
<button name="button" value="save" class="formbutton" id="saveButton">save</button>
<button name="button" value="cancel" class="formbutton" id="cancelButton">cancel</button>
</div>
}
</div>
<div id="NonEditable">
<div>
<div>
@Html.LabelFor(m => m.Username, new { @class = "entryFieldLabel" })
</div>
</div>
<div>
<div>
<button name="button" value="edit" class="formbutton" id="editButton" type="button">edit</button>
</div>
</div>
</div>
和C#方法:
public ActionResult EditUser(AzaraUserModel AzaraUserModel, string button)
{
if (button == Tumormodelle.Properties.Resources.Save)
{
if (ModelState.IsValid)
{
azaraUserManagement.Update(AzaraUserModel.Username, AzaraUserModel.Title, AzaraUserModel.FirstName, AzaraUserModel.LastName, AzaraUserModel.EMailAddress, null, AzaraUserModel.Phone, AzaraUserModel.UserID, (byte)AzaraUserModel.ShowMail.ID);
ViewBag.Message = Tumormodelle.Properties.Resources.Personal_Data_Changed;
ViewBag.Editable = true;
}
else ViewBag.Editable = false;
BindShowMailList();
return PartialView("AccountInformation", AzaraUserModel);
}
else
{
return RedirectToAction("OpenAccountInformation", "ManageUsers");
}
}
public ActionResult UserInformation()
{
return View("UserInformation");
}
public PartialViewResult OpenAccountInformation()
{
AzaraUserModel AzaraUserModel = new AzaraUserModel(azaraUserManagement.GetSingle(AzaraSession.Current.UserComparison.GetUser().Id));
BindShowMailList();
ViewBag.Editable = true;
return PartialView("AccountInformation", AzaraUserModel);
}
編輯:在Chrome調試器的幫助下,我發現,在表單變爲submited的時候,錯誤就被拋出了。
沒有幫助:(。檢查與Visual Studio的調試器似乎c#類甚至沒有擊中,因爲我點擊'保存'按鈕。另一方面,'取消'按鈕工作正常,但不需要調用服務器。 – Jastol 2014-11-05 10:37:55