简体   繁体   English

REST API复杂类型参数没有填充属性

[英]REST api complex type parameter doesn't have properties populated

I'm trying to call a REST api via POST and passing in my parameter as a json array (username and password). 我试图通过POST调用REST api,并将参数作为json数组(用户名和密码)传递。 The object is instantiated, but the properties are not. 该对象被实例化,但属性未被实例化。 Where am I going wrong? 我要去哪里错了?

[HttpPost]
public AuthenticationResult Authenticate([FromBody]LoginModel login)
{
    try
    {
        if (login == null)
            throw new ArgumentNullException("Username and Password are required for authentication.");

        AuthenticationService authService = new AuthenticationService();
        AuthenticationResult result = authService.IsAuthenticated(login);

        if (result.IsAuthenticated)
            return result;
        else
        {
            return new AuthenticationResult()
            {
                IsAuthenticated = false,
                User = new User()
                {
                    UserId = login.Username
                }
            };
        }
    }
    catch(Exception ex)
    {
        return new AuthenticationResult()
        {
            IsAuthenticated = false,
            Message = ex.Message
        };
    }
}

And I'm testing from Fiddler... 我正在Fiddler进行测试...

I'm set to "POST", url: htp://localhost:2134/api/login/authenticate 我设置为“ POST”,网址:htp:// localhost:2134 / api / login / authenticate

Request Headers: 请求标头:

User-Agent: Fiddler
Host: localhost:42761
Content-Length: 73
Content-Type: application/json
Accept: application/json

Request Body: 请求正文:

{
"Username": "ganders",
"Password": "myHashedPassword"
}

Edit: According to this article , it would appear as though I have everything structured correctly... 编辑:根据这篇文章 ,似乎一切结构都正确……

Edit2: LoginModel 编辑2:LoginModel

[Serializable]
public class LoginModel
{
    public string Username { get; set; }

    public string Password { get; set; }

    public string AccessToken { get; set; }
}

必须从我的LoginModel类中删除[Serializable]属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM