簡體   English   中英

ASP.NET 核心 JWT 令牌無法通過 HttpClient 工作

[英]ASP.NET Core JWT Token not working via HttpClient

我是 JWT 身份驗證的新手,我已經使用此處的教程配置了我的 ASP.NET Core 3 API 進行令牌身份驗證:

https://chrissinty.com/securing-your-blazor-apps-authentication-with-clientside-blazor-using-webapi-aspnet-core-identity/

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM