xamarin - Identitty4 Access Token from Android and token lenght
问题描述
I have a console application where I use to test some code. In my console app everything works as expected. Same code implemented in the xamarin forms and I get an un tI sent a request to identity4 to get a token and it works receiving a token 685 characters of length. The same code that I use in the console is implemented in android on a button click action and I receive the token as well. I try to use the access token to my protected api and I get Unauthorized. I try to use the token in postman and I get Unauthorized. If I use the token received in my console in the postman it works! What I realize is that in the console app the access token have a length of 865 characters and in android the access token have a length of 864. I checked the length from the content which I receive before apply NewtonSoft Json converter
My console code:
private static async Task Main()
{
Console.WriteLine("Request For a token");
//var handler = new HttpClientHandler();
//handler.ClientCertificateOptions = ClientCertificateOption.Manual;
//handler.SslProtocols = SslProtocols.Tls12;
//handler.ClientCertificates.Add(new X509Certificate2("rsaCert.pfx","1234"));
//var client = new HttpClient(handler);
// discover endpoints from metadata
var client = new HttpClient();
//client.BaseAddress = new Uri("http://localhost:5000");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "test"),
new KeyValuePair<string, string>("password", "test"),
new KeyValuePair<string, string>("scope", "catalogapi1"),
new KeyValuePair<string, string>("client_id", "ro.client"),
new KeyValuePair<string, string>("client_secret", "secret")
});
var result = await client.PostAsync("http://localhost:5000/connect/token", content);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
ServerReponse responseToken = JsonConvert.DeserializeObject<ServerReponse>(resultContent);
Console.WriteLine("Token Lenght: "+ responseToken.access_token.Length);
Console.WriteLine("\n\n");
// call api
var apiClient = new HttpClient();
apiClient.SetBearerToken(responseToken.access_token);
var response = await apiClient.GetAsync("http://localhost:5100/api/v1/products");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content2 = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content2));
}
Console.WriteLine("Press a key to close the application");
Console.ReadLine();
}
And in my Android event click action:
async void OnButtonClicked(object sender, EventArgs args)
{
var client = new HttpClient();
//client.BaseAddress = new Uri("http://localhost:5000");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "test"),
new KeyValuePair<string, string>("password", "test"),
new KeyValuePair<string, string>("scope", "test"),
new KeyValuePair<string, string>("client_id", "ro.client"),
new KeyValuePair<string, string>("client_secret", "secret")
});
var result = await client.PostAsync("http://10.0.2.2:5000/connect/token", content);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
ServerReponse responseToken = JsonConvert.DeserializeObject<ServerReponse>(resultContent);
// call api
var apiClient = new HttpClient();
apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", responseToken.access_token.Trim());
// apiClient.SetBearerToken(responseToken.access_token);
var response = await apiClient.GetAsync("http://10.0.2.2:5100/api/v1/products");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content2 = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content2));
}
Console.WriteLine("Press a key to close the application");
Console.ReadLine();
}
public class ServerReponse
{
public string access_token { get; set; }
public string expires_in { get; set; }
public string token_type { get; set; }
public string scope { get; set; }
}
解决方案
推荐阅读
- doctrine - 添加基于条件的选择到学说查询构建器
- r - geom_density y 轴高于 1
- angular - 内部服务器错误。尝试使用角度和猫鼬从 mlab 中的数组中删除一个元素
- spring - 在 Spring Boot jar 包中使用 JWT 部署 Angular 应用程序
- robotframework - 机器人框架上的数据驱动测试用例
- video - 将 NTSC DVD 转换为原始 HEVC 文件 - 29.97 或 23.967 fps?
- sql - 如何访问不在第三个联结表中的两个多对多表中的记录?
- javascript - 当您可以在 Javascript 中全局调用函数时,为什么要使用回调?
- ios - 带有集合单元的 UIImageViews 在 viewDidLoad() 上未正确调整大小
- r - R - 如何计算时间序列数据中分类变量的间隔平均值?