gpt4 book ai didi

c# - 模型绑定(bind) CSP 报告 json

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:48 24 4
gpt4 key购买 nike

我正在尝试创建一个 URL,我的网站可以将 CSP 违规发布到该 URL,但我发现如果没有我自己的自定义模型 Binder ,则很难进行模型绑定(bind)。

CSP 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"
}
}

这里有两个主要问题。访问嵌套属性,那么您将如何访问 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; }
}

如何访问包含“-”字符的参数。

以下仅绑定(bind)“referrer”属性:

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; }
}

最佳答案

就我个人而言,我只是跳过了整个绑定(bind)机制,直接进入了内容主体:

    [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;
}
}

关于c# - 模型绑定(bind) CSP 报告 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com