2016-05-03 3 views
1

Я пытаюсь сделать URL-адрес, на котором мой сайт может публиковать нарушения CSP, но мне очень сложно моделировать привязку без моего собственного настраиваемого связующего.Модель привязки отчета CSP json

Что JSON СНТ выглядит следующим образом:

{ 
    "csp-report": { 
     "document-uri": "https://example.com/foo/bar", 
     "referrer": "https://www.google.com/", 
     "violated-directive": "default-src self", 
     "original-policy": "default-src self; report-uri /csp-hotline.php", 
     "blocked-uri": "http://evilhackerscripts.com" 
    } 
} 

Есть 2 основные проблемы здесь. Доступ к вложенным свойствам, так как вы можете получить доступ к свойствам внутри объекта csp-report.

Эта модель возвращает только нуль:

public class CspReportRequest 
{ 
    [JsonProperty(PropertyName = "csp-report")] 
    public CspReport CspReport { get; set; } 
} 

public class CspReport 
{ 
    [JsonProperty(PropertyName = "document-uri")] 
    public string DocumentUri { get; set; } 

    [JsonProperty(PropertyName = "referrer")] 
    public string Referrer { get; set; } 

    [JsonProperty(PropertyName = "violated-directive")] 
    public string ViolatedDirective { get; set; } 

    [JsonProperty(PropertyName = "original-policy")] 
    public string OriginalPolicy { get; set; } 

    [JsonProperty(PropertyName = "blocked-uri")] 
    public string BlockedUri { get; set; } 
} 

Как получить доступ к параметрам, которые содержат «-» символ.

Следующие только связывает свойство "реферера":

JSON:

{ 
    "document-uri": "https://example.com/foo/bar", 
    "referrer": "https://www.google.com/", 
    "violated-directive": "default-src self", 
    "original-policy": "default-src self; report-uri /csp-hotline.php", 
    "blocked-uri": "http://evilhackerscripts.com" 
} 

модель:

public class CspReport 
{ 
    [JsonProperty(PropertyName = "document-uri")] 
    public string DocumentUri { get; set; } 

    [JsonProperty(PropertyName = "referrer")] 
    public string Referrer { get; set; } 

    [JsonProperty(PropertyName = "violated-directive")] 
    public string ViolatedDirective { get; set; } 

    [JsonProperty(PropertyName = "original-policy")] 
    public string OriginalPolicy { get; set; } 

    [JsonProperty(PropertyName = "blocked-uri")] 
    public string BlockedUri { get; set; } 
} 

ответ

0

Лично я просто пропустил весь обязательный механизм и пошел прямо к содержанию тел .:

[HttpPost] 
    public async Task<bool> Post() 
    {   
     try 
     { 
      string content = await Request.Content.ReadAsStringAsync().ConfigureAwait(false); 
      CspReportRequest cspReport = JsonConvert.DeserializeObject<CspReportRequest>(content); 

      //Do Stuff Here!! 

      return true; 
     } 
     catch(Exception ex) 
     { 
      return false; 
     } 
    } 
Смежные вопросы