[英]How to send bearer token through header of axios call in react redux
[HttpPost("xxxxxxxxxx")]
[Authorize(Roles = "xxxxxxxxx")]
public IActionResult Post([FromBody]xxxxxxx xxxxxxxxxxxxxx)
{
if (s == null)
{
}
}
上面的代码是属性 Authorize(Roles ="Director")。 我的申请角色是学生、主任、经理。 这个特殊的方法只能由主管访问,对于上述方法,它会返回 401 unauthorized。
export function xxxxxxxxxxxx(token, formValues) {
return axios.post(`${ROOT_URL}/xxxxxxx/xxxxxxxx`, formValues);
}
}
我不确定如何通过 axios 调用在 header 中发送不记名令牌。
您只需要传递Bearer jwt令牌作为第三个参数。
axios.post(url, data, {
'headers': {
'Authorization': 'Bearer ' + jwtStr
});
如果向Web API控制器发送请求,则需要在标头Authorization中发送授权令牌,例如:“ Bearer xxxxxxxx”。
您可以尝试在 API const 声明中配置它,如下所示:
apiClient.ts 内容:
import axios from 'axios';
const token = 'POINT YOUR TOKEN LOCATION HERE'
const api = axios.create({
baseURL: 'http://127.0.0.1:3333'
});
axios.defaults.headers = {
Authorization: 'Bearer ' + token
}
export default api;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.