[英]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.