5

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

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

授权,基本 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

并在 URL 中使用以下参数:

client_id=后端客户端

范围=读取

grant_type=密码

用户名=管理员

密码=管理员

所以它就像: 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>

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

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

谢谢。

4

1 回答 1

1

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

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

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

于 2016-10-06T14:23:21.473 回答