简体   繁体   English

从 Xamarin 应用程序中的身份服务器获取 OAuth 访问令牌

[英]Obtaining OAuth access token from identity server within Xamarin application

Within a Xamarin application I have created, I'm trying to connect to a Web API which I'm running locally in a separate solution.在我创建的 Xamarin 应用程序中,我正在尝试连接到 Web API,我在单独的解决方案中本地运行。 In order to successfully connect to this API I have to retrieve an access token from the identity server endpoint (OAuth 2.0).为了成功连接到这个 API,我必须从身份服务器端点(OAuth 2.0)检索访问令牌。

I'm currently finding it very difficult to find a solution that allows me to retrieve this token.我目前发现很难找到允许我检索此令牌的解决方案。

In existing .NET projects I have used the following code to retrieve the access token but in the Xamarin App this does not work, it hits the first line and comes back 5 minutes later saying it was cancelled:在现有的 .NET 项目中,我使用以下代码检索访问令牌,但在 Xamarin 应用程序中这不起作用,它到达第一行并在 5 分钟后返回说它已被取消:

try
{
   var disco = await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
   {
       Address = clientCredentials.Path,
       Policy =
       {
          RequireHttps = false
       }
   });

   if (disco.IsError) Console.WriteLine(disco.Error);

   var tokenResponse = await httpClient.RequestClientCredentialsTokenAsync(newClientCredentialsTokenRequest
   {
       Address = "https://<local-machine-ip>:<port-of-auth-localhost>/connect/token",
       ClientId = clientCredentials.ClientId,
       ClientSecret = clientCredentials.ClientSecret,
       Scope = clientCredentials.Scope
   });

   if (tokenResponse.IsError) Console.WriteLine(tokenResponse.Error);

   httpClient.SetBearerToken(tokenResponse.AccessToken);
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}

The closest I have got is through this code, but this returns the 'Error: TrustFailure (Authentication failed, see inner exception.)':我得到的最接近的是通过此代码,但这会返回“错误:TrustFailure(身份验证失败,请参阅内部异常。)”:

var client = new RestClient("https://<local-machine-ip>:<port-of-auth-localhost>/connect/token");
var request = new RestRequest(Method.POST);

request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=xxxx&client_secret=xxxxx&scope=xxxxx", ParameterType.RequestBody);

IRestResponse response = await client.ExecuteAsync(request);

I'm running out of ideas at this point, does anyone know of a solution?在这一点上我的想法已经用完了,有人知道解决方案吗?

Just to note I am running xamarin.android the android emulator.请注意,我正在运行 xamarin.android android 仿真器。 The code shown above currently sits in a class within the ClassLibrary of the xamarin.forms solution上面显示的代码当前位于 xamarin.forms 解决方案的 ClassLibrary 中的 class

locahost is the emulator itself, not the PC that is hosting the emulator, use 10.0.2.2 instead to "reach" the hosting PC. locahost是模拟器本身,而不是托管模拟器的 PC,请使用 10.0.2.2 来“访问”托管 PC。

Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface.另请注意,您的开发机器上的地址 127.0.0.1 对应于模拟器自己的环回接口。 If you want to access services running on your development machine loopback interface (aka 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead.如果您想访问在您的开发机器环回接口(也就是您机器上的 127.0.0.1)上运行的服务,您应该使用特殊地址 10.0.2.2 来代替。

Set up Android Emulator networking: https://developer.android.com/studio/run/emulator-networking设置 Android 仿真器网络: https://developer.android.com/studio/run/emulator-networking

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

相关问题 在.NET Core Web应用程序中存储Identity Server的访问令牌 - Storing access token from Identity Server in NET Core web application 有没有办法在不使用端点的情况下从 Identity Server 中生成访问令牌? - Is there a way to generate an access token from within Identity Server without using the endpoints? 从桌面应用程序从 Google Drive API 获取 access_token - Obtaining access_token from Google Drive API from Desktop application 身份服务器 3 访问令牌验证库无法验证从身份服务器 4 生成的令牌 - Identity Server 3 Access Token Validation Library Cannot Validates Tokens Generated from Identity Server 4 检查访问令牌是否有效 - Identity Server - Check if access token is valid - Identity Server 身份服务器 4:向访问令牌添加声明 - Identity Server 4: adding claims to access token Identity Server 4 无法验证我的访问令牌 - Identity Server 4 Cant Validate My Access Token Identity Server 4 为后续请求存储访问令牌 - Identity Server 4 store access token for subsequent requests 控制台应用程序中的 OAuth2 访问令牌 - OAuth2 Access token in console application 如何在ASP Net 4.7 MVC应用程序中获取Identity Server 4的access_token - How to get Identity Server 4's access_token in ASP Net 4.7 MVC Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM