我在使用 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
它返回 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/