我在使用axios的vuejs中有这个
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
这是后端
[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;
}
答案 0 :(得分:0)
您的邮递员帖子使用参数传递用户名和密码?quad=YGOP&password=P@ssw0rd
,而您的axios帖子使用有效负载/正文传递数据
data: {
quad: this.quad,
password: this.password
}
尝试更改此行:
url: 'http://localhost:64427/api/Authenticate/Token?quad=' + this.quad + '&password=' + this.password,
然后将data
设置为{}
答案 1 :(得分:0)
您需要更改后端以接受从客户端发送的模型,您可以通过添加包含2个属性的类LoginData并将其放入Login函数并使用FromBody属性将其映射到身体而不是网址。
[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; }
}