小编Mic*_*dej的帖子

如何强制Newtonsoft Json序列化所有属性?("指定"属性的奇怪行为)

各位程序员,我在Newtonsoft.Json中遇到过一个奇怪的行为.

当我试图序列化一个看起来像这样的对象:

public class DMSDocWorkflowI
{
    [JsonProperty("DMSDocWorkflowIResult")]
    public bool DMSDocWorkflowIResult { get; set; }

    [JsonProperty("DMSDocWorkflowIResultSpecified")]
    public bool DMSDocWorkflowIResultSpecified { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用这个简单的调用,没有自定义转换器/绑定器/合同解析器:

var testObject = new DMSDocWorkflowI();
var json = JsonConvert.SerializeObject(testObject, Formatting.Indented);
Run Code Online (Sandbox Code Playgroud)

或者甚至JToken.FromObject(...)我总是只得到一处房产:

{
   "DMSDocWorkflowIResultSpecified": false
}
Run Code Online (Sandbox Code Playgroud)

当我附加跟踪编写器时,它只捕获这个:

[0]: "2016-08-30T11:06:27.779 Info Started serializing *****DMSDocWorkflowI. Path ''."
[1]: "2016-08-30T11:06:27.779 Verbose IsSpecified result for property 'DMSDocWorkflowIResult' on *****DMSDocWorkflowI: False. Path ''."
[2]: "2016-08-30T11:06:27.779 Info Finished serializing *****.DMSDocWorkflowI. Path ''."
[3]: "2016-08-30T11:06:27.780 Verbose Serialized JSON: \r\n{\r\n  \"DMSDocWorkflowIResultSpecified\": false\r\n}"
Run Code Online (Sandbox Code Playgroud)

所以似乎Newtonsoft.Json有些神奇地对待这个"Specified"属性.我能把它关掉吗?我在生成的JSON中需要这些属性以及这些名称.

c# serialization json.net

13
推荐指数
1
解决办法
2563
查看次数

验证Live.com(Microsoft帐户)JWT令牌

研究员,

我目前正在努力使用Web Api 2中的Microsoft帐户JWT令牌验证.我已经找到了OWIN中间件(NuGet包Microsoft.Owin.Security.Jwt),这里是我的Startup.cs配置的代码:

    public void ConfigureAuth(IAppBuilder app)
    {
        var sha256 = new SHA256Managed();
        var secretBytes = System.Text.Encoding.UTF8.GetBytes(@"(My app client secret)" + "JWTSig");
        byte[] signingKey = sha256.ComputeHash(secretBytes);

        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AllowedAudiences = new[] { "(My API's domain )" },
                IssuerSecurityTokenProviders =
                    new[]
                            {
                                new SymmetricKeyIssuerSecurityTokenProvider(
                                    "urn:windows:liveid", signingKey)
                            }
            });
    }
Run Code Online (Sandbox Code Playgroud)

我在这里发现了这个片段:

http://code.lawrab.com/2014/01/securing-webapi-with-live-id.html

JWT令牌使用Live SDK从我的Windows应用商店应用客户端发送.我正在发送身份验证令牌,而不是访问令牌,所以我确定它是JWT.使用像这样的在线调试器:http://jwt.io/我能够成功解码标头和有效负载部分,但我找不到验证签名的方法.发送具有该JWT的请求时,我的Web API的调试输出是:

Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware Error: 0 : Authentication failed
System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier
    (
    IsReadOnly = False, …

c# security jwt owin asp.net-web-api2

8
推荐指数
1
解决办法
1288
查看次数

有没有一种在Visual Studio中编写UTF-8八位字节的简单方法?

我有一个问题,我需要在C++源代码中使用标准字符类型的UTF-8编码字符串,如下所示:

char* twochars = "\xe6\x97\xa5\xd1\x88";
Run Code Online (Sandbox Code Playgroud)

通常,如果我想写一个UTF-8字符,我需要使用上面的八位字节.Visual Studio中有什么东西(我使用VS 2013 Ultimate)可以让我只编写例如"ĄĘĆŻ"并自动将每个字符转换为多个UTF-8八位字节,如上例所示?或者我应该使用const wchar_t*并找到一个可以将宽字符串转换为UTF-8编码的标准字符串字符串的库?

如果没有这样的东西,你能为此建议任何外部软件吗?我真的不想浏览每个符号/非拉丁字母的字符映射.

对不起我的英文,提前致谢.

c++ encoding utf-8 visual-studio

7
推荐指数
1
解决办法
4677
查看次数

如何完全清除/设置WinRT的RichEditBox文本?

如何完全覆盖或清除WinRT的RichEditBox的文本(和格式)?

我问,因为它的Document属性的方法SetText似乎只是附加新文本.

因此"绑定"如下:

void Vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Content")
        richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, Vm.Content);
}

private void ContentChanged(object sender, RoutedEventArgs e)
{
    RichEditBox box = (RichEditBox)sender;

    string content;
    box.Document.GetText(Windows.UI.Text.TextGetOptions.None, out content);

    Vm.Content = content;
}
Run Code Online (Sandbox Code Playgroud)

这里Vm_PropertyChanged只是监听的变化Content视图模型的字符串属性,并ContentChanged为处理器TextChanged的RichEditBox的情况下,将创建一个无限循环不断追加"\ r"把Vm.Content和框的文本本身.当您TextGetOptions.None使用TextGetOptions.FormatRtfViewModel 替换Content属性时,会更加混乱,添加看起来像空RTF段落的内容.

这是ViewModel中的Content属性定义,因此您可以确保一切正常:

    /// <summary>
    /// The <see cref="Content" /> property's name.
    /// </summary>
    public const string ContentPropertyName = "Content";

    private string _content;

    /// <summary>
    /// Sets …
Run Code Online (Sandbox Code Playgroud)

.net c# windows-runtime

5
推荐指数
1
解决办法
3197
查看次数