首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >邮局在邮差工作,但在浏览器中不工作。

邮局在邮差工作,但在浏览器中不工作。
EN

Stack Overflow用户
提问于 2018-04-18 11:24:58
回答 2查看 5.8K关注 0票数 3

我用axios在vuejs中找到了这个

代码语言:javascript
复制
  axios({
    method: 'post',
    url: 'http://localhost:64427/api/Authenticate/Token',
    headers: {
      'Content-type': 'application/json'
    },
    data: {
      quad: this.quad,
      password: this.password
    }
  })
  .then(response => {
    console.log(response.header.token);
  })

当我在postman中使用端点时,它可以工作。

http://localhost:64427/api/Authenticate/Token?quad=YGOP&password=P@ssw0rd

但是,当我使用axios发布时,我可以编写404错误代码。

这是后端

代码语言:javascript
复制
[HttpPost]
[ActionName("Token")]
[BasicAuthentication]
public HttpResponseMessage Login(string quad, string password)
{
    bool isAuthenticated = EmployeeSecurity.Login(quad, password);
    if(isAuthenticated)
    {
        if (Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            var basicAuthenticationIdentity = Thread.CurrentPrincipal.Identity;
            if (basicAuthenticationIdentity != null)
            {
                var username = basicAuthenticationIdentity.Name;
                return GetAuthToken(username);
            }
            return null;

        }
    }

    return null;
}
EN

回答 2

Stack Overflow用户

发布于 2018-04-19 20:26:10

您需要更改后端以接受从客户端发送的模型,可以通过添加包含2个属性的类LoginData并将其放入Login函数中,并使用FromBody属性从正文而不是url映射它。

代码语言:javascript
复制
        [HttpPost]
        [ActionName("Token")]
        [BasicAuthentication]
        public HttpResponseMessage Login([FromBody] LoginData login)
        {
            bool isAuthenticated = EmployeeSecurity.Login(login.quad, login.password);
            if (isAuthenticated)
            {
                if (Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    var basicAuthenticationIdentity = Thread.CurrentPrincipal.Identity;
                    if (basicAuthenticationIdentity != null)
                    {
                        var username = basicAuthenticationIdentity.Name;
                        return GetAuthToken(username);
                    }
                    return null;

                }
            }

            return null;
        }

        public class LoginData
        {
            public string quad { get; set; }

            public string password { get; set; }
        }
票数 1
EN

Stack Overflow用户

发布于 2018-04-18 11:28:54

邮递员帖子使用参数传递用户名和密码?quad=YGOP&password=P@ssw0rd,而axios使用有效载荷/正文传递数据

代码语言:javascript
复制
data: {
  quad: this.quad,
  password: this.password
}

试着改变这一行:

代码语言:javascript
复制
url: 'http://localhost:64427/api/Authenticate/Token?quad=' + this.quad + '&password=' + this.password,

然后将data设置为{}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49898530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档