[英]ASP.NET Core JWT Token not working via HttpClient
我是 JWT 身份驗證的新手,我已經使用此處的教程配置了我的 ASP.NET Core 3 API 進行令牌身份驗證:
I've added the [Authorize]
attribute to the controller, and using Postman I can login and get a token, and use the token via Postman to retrieve the data from the controller - all works as expected to here.
My issue is when I login using the C# HttpClient
via a Xamarin app, and then I use the the token that is sent back I get a 401 error accessing the controller either via Postman or HttpClient
.
我正在使用的代碼如下:
var client = new HttpClient();
var model = new JwtLoginModel()
{
Email = email,
Password = password,
RememberMe = false
};
var json = JsonConvert.SerializeObject(model);
HttpContent httpContent = new StringContent(json);
var response = await client.PostAsync(Constants.BaseApiAddress + "api/Login", new StringContent(json, Encoding.UTF8, "application/json"));
var content = await response.Content.ReadAsStringAsync();
JObject jwtDynamic = JsonConvert.DeserializeObject<dynamic>(content);
var accessToken = jwtDynamic.Value<string>("token");
使用通過 Postman (和HttpClient
)返回的accessToken
,兩者都返回 401 錯誤。
誰能建議我做錯了什么?
謝謝大家的幫助
最后,我決定使用運行良好的 Identity Server 4 實現 go 更容易
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.