c# - 使用 Xamarin.Auth 进行 OAuth2 身份验证 - 用户名和密码?

标签 c# xamarin oauth-2.0 xamarin.forms xamarin.auth

我在使用 Xamarin.Auth 进行 OAuth2 身份验证时遇到了一些问题。

我从 POSTMAN 发送 token 请求,通过 GET 方法到我的后端 URL: http://example.com/backend/oauth/token 通过添加到标题:

Authorization, Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

并在 URL 中使用以下参数:

client_id=backendClient

scope=read

grant_type=password

username=admin

password=admin

所以它是这样的: http://example.com/backend/oauth/token?client_id=backendClient&scope=read&grant_type=password&username=admin&password=admin

它返回 JSON,如下所示:

{
"access_token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"token_type": "bearer",
"refresh_token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"expires_in": 9999,
"scope": "read"
}

我想从移动应用程序(使用 Xamarin.Forms 应用程序)对我的后端服务进行身份验证,并且我尝试使用 Xamarin.Auth 对我的后端进行身份验证 - https://developer.xamarin.com/guides/xamarin-forms/web-services/authentication/oauth/

确实使用 OAuth2Authenticator,它至少能够在返回时“ping”我的后端(通过放置 URL、clientIde 等):

<UnauthorizedException>
    <error>unauthorized</error>
    <error_description>Full authentication is required to access this resource</error_description>
</UnauthorizedException>

但是消息以错误的方式发送 - 至少我是这样认为的,因为 header 中没有授权 + 我没有看到任何可能将用户名和密码传递给 OAuth2Authenticator .

有谁知道我该怎么做?或者我应该使用其他东西而不是 Xamarin.Auth?

谢谢。

最佳答案

我是这样做的(不使用 Xamarin.Auth):

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenGoesHere);

然后您可以添加任何您喜欢的内容和 Get/Post/PutAsync,无论您需要什么。

关于c# - 使用 Xamarin.Auth 进行 OAuth2 身份验证 - 用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38247413/

相关文章:

c# - 多个列表框同时滚动?

c# - 为 UserControl 填充模型的正确方法是什么?

rsa - 使用 at_hash 验证访问 token

javascript - Syncfusion 控件未在 iOS native UIWebView 上加载

c# - 如何在 Visual Studio 2019 中使用 Xamarin Forms(UWP) 和 C# 创建 Web 应用程序

android - 如何使用访问 token 获取 OAuth 2 刷新 token

rest - 使用来自数据库的 oAuth2 安全凭证的 Spring Boot Rest 服务

c# - 我可以在同一个项目中有多个 DbContexts

c# - 使用c#创建带标题的word文档

c# - Android 在间隔后更新谷歌地图