Bonjour tout le monde !
Dans mon application je fais actuellement un appel ajax en post en sérialisant mon objet compagnie afin de sauvegarder l'édition dans une action de mon controller
Pour ce faire j'utilise une classe JsonModelBinder
qui récupère mon Json et le desérialise en un objet compagnie afin de pouvoir le manipuler dans une action de mon controller
Donc Mon json est de la forme de :
Code texte : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 {"CompanyName":"Loremipsum","CompanyNumber":"111150","CompanyLeftPostalCode":"h1y","CompanyRightPostalCode":"3V8","CompanyProvince":"Loremipsum","CompanyStreet":"Loremipsum","CompanyStreetSuite":"Loremipsum","CompanyPhone":"514-222-7894","CompanyCity":"Loremipsum"}
mon appel Ajax :
Code js : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 function SaveCompany() { var model = new Object(); // population de mon objet var URL = "SaveCompany"; $.post(URL, JSON.stringify(model), function (data) { $("#divResult").html(data); //Traitement }); }
et voici le controller
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 // // POST: [HttpPost] //[ValidateAntiForgeryToken] [JsonModelBinder(ActionParameterName = "model", ActionParameterType = typeof(Company))] public ActionResult SaveCompany(Company model) { // Traitment return PartialView("_CompanyDetailsPartial", model); }
ça marche ... Seulement si je veux ajouter AntiForgeryToken mon JsonModelBinder n'est plus capable de desérialiser mon json
voici la classe
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class JsonModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if(!IsJSONRequest(controllerContext)) { return base.BindModel(controllerContext, bindingContext); } // Get the JSON data that's been posted var request = controllerContext.HttpContext.Request; var jsonStringData = new StreamReader(request.InputStream).ReadToEnd(); // Use the built-in serializer to do the work for us //return new JavaScriptSerializer() // .Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType); // -- REQUIRES .NET4 // If you want to use the .NET4 version of this, change the target framework and uncomment the line below // and uncomment the above return statement return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType); } private static bool IsJSONRequest(ControllerContext controllerContext) { var contentType = controllerContext.HttpContext.Request.ContentType; return contentType.Contains("application/json"); } }
j'ai récupérer le code source suivant :
http://www.codeproject.com/Articles/...i-Forgery-Toke
afin de faire cette appel :
Code js : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 $.postAntiForgery(URL, JSON.stringify(model), function (data) { ...
avec un @Html.AntiForgeryToken() dans le form
le json est le suivant :
{"CompanyName":"DicomExpress","CompanyNumber":"111150","CompanyLeftPostalCode":"h1t","CompanyRightPostalCode":"3a8","CompanyProvince":"QC","CompanyStreet":"6-6855 35E","CompanyStreetSuite":"","CompanyPhone":"514-222-7894","CompanyCity":"Montréal"}&__RequestVerificationToken=AOM1R5QxOobMtxcApkoPx1zYHkAd4RC9hsaNF0rnubIizBtQQaawOLAs5FawMdtzqMxx2PDdQDEwd1gkwNs5pPsOEEF0Qen6_047DpkGLEPuDdWSs5CBEquAhI4Lh3SaTV4m7lh4q1Naasdh0EbGsg2
mais mon JsonModelBinder ne desérialise pas
dans le lien je n'utilise pas :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public abstract class AntiForgeryControllerBase : Controller{ private readonly ValidateAntiForgeryTokenAttribute _validator; private readonly AcceptVerbsAttribute _verbs; protected AntiForgeryControllerBase(HttpVerbs verbs, string salt) { this._verbs = new AcceptVerbsAttribute(verbs); this._validator = new ValidateAntiForgeryTokenAttribute() { Salt = salt }; } protected override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); string httpMethodOverride = filterContext.HttpContext.Request.GetHttpMethodOverride(); if (this._verbs.Verbs.Contains (httpMethodOverride, StringComparer.OrdinalIgnoreCase)) { this._validator.OnAuthorization(filterContext); } }}
car j'ai l'erreur suivante :
Erreur 1 'System.Web.Mvc.ValidateAntiForgeryTokenAttribute.Salt' est obsolète*: 'The 'Salt' property is deprecated. To specify custom data to be embedded within the token, use the static AntiForgeryConfig.AdditionalDataProvider property.
Je ne vois pas comment utiliser AntiForgeryConfig.AdditionalDataProvider...
Est ce que quelqu'un pourrait m'aiguiller ? Merci d'avance !
Partager