简体   繁体   English

无法通过具有JWT身份验证的API进行授权Im正在使用Xamarin

[英]Cant get Authorized with my API that has a JWT authentication Im Working With Xamarin

Well Im new in Xamarin and I'm developing and App, the authentication is JWT based. 好吧,我是Xamarin新手,我正在开发App,身份验证是基于JWT的。 Im using a HttpClient and setting the AuthenticationHeaders but It always returns Unauthorized when I try it on Postman it Works but I can't make it work in my app. 我正在使用HttpClient并设置AuthenticationHeaders但是在Postman上尝试运行时它总是返回Unauthorized ,但是我无法在我的应用程序中使其正常工作。

Here is how im trying to do it: 这是即时通讯尝试执行的操作:

var client = new HttpClient(new HttpClientHandler());
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("JWT", accessToken);
client.BaseAddress = new Uri(urlBase);
var url = string.Format("{0}{1}", servicePrefix, controller);
var response = await client.GetAsync(url);

Try something like this 试试这个

using (var client = new HttpClient())
        {
            var uri = new Uri(string.Format($"{<yourURLString>}", string.Empty));
            var jsonTransport = "";
            var jsonPayload = new StringContent(jsonTransport, Encoding.UTF8, "application/json");
            //client.DefaultRequestHeaders.Add("Content-type", "application/json");
            client.DefaultRequestHeaders.Add("Authorization", "JWT " + accessToken);
            var response = await client.PostAsync(uri, jsonPayload);
            string responseContent = await response.Content.ReadAsStringAsync();
        }

then deserialize the responseContent to your object using JsonConvert.DeserializeObject Note: Below are code samples, edit to your own objects 然后使用JsonConvert.DeserializeObject将responseContent反序列化到您的对象注意:以下是代码示例,请编辑到您自己的对象

SubscriptionResponse profileResponse = JsonConvert.DeserializeObject<SubscriptionResponse>(responseContent);

then if your method returns something, use the return statement. 然后,如果您的方法返回了某些内容,请使用return语句。 Something like this 像这样

return profileResponse.Data.Subscriptions;

If you're using a get, this could be a guide 如果您使用的是get,这可能是一个指南

var uri = new Uri(string.Format($"{<yourURLHere>}", string.Empty));
client.DefaultRequestHeaders.Add("Authorization", "JWT " + accessToken);
var httpResponse = await client.GetAsync(uri);
var responseContent = await httpResponse.Content.ReadAsStringAsync();

then deserialize your string response Note: this is a sample - edit to your model (You may use PostMan to get the response format in JSON and model it in C#) 然后反序列化您的字符串响应注意:这是一个示例-编辑模型(您可以使用PostMan获取JSON中的响应格式并在C#中对其进行建模)

var UserDetailResponse = JsonConvert.DeserializeObject<UserDetail>(responseContent);
return UserDetailResponse;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已对JWT授权的Web API控制器进行单元测试 - Unit Testing a Web API controller which Has Been Authorized with JWT Web API 核心 JWT 身份验证不起作用 - Web API Core JWT Authentication is not working Facebook Graph API获取对我的应用进行授权的那些用户的所有授权朋友 - Facebook Graph API Get All Authorized Friends of those Users who authorized my app 我试图从我的 xamarin 项目的 azure 数据库中取回数据 - Im trying to get the data back from azure database for my xamarin project 使用System.IdentityModel.Tokens.Jwt在Web API中进行JWT身份验证 - JWT Authentication in Web API using System.IdentityModel.Tokens.Jwt 在 .NET 7 MAUI 中使用 JWT 认证消费 web api? - Consuming web api with JWT authentication in .NET 7 MAUI? 如何将JWT身份验证与Web API集成在一起? - How to integrate JWT authentication with Web API? ASP.NET Web API 的 JWT 身份验证 - JWT authentication for ASP.NET Web API JWT 承载身份验证和授权不起作用,因为 TokenValidationParameters - JWT Bearer Authentication and Authorization not working, because of TokenValidationParameters Xamarin与POST,asp.net-web-api-2的Https连接,“身份验证或解密失败” - Xamarin connection Https with POST, asp.net-web-api-2, “The authentication or decryption has failed”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM