【问题标题】:Using Xamarin.Auth for OAuth2 authentication - username and password?使用 Xamarin.Auth 进行 OAuth2 身份验证 - 用户名和密码?
【发布时间】:2016-11-09 21:16:54
【问题描述】:

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

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

授权,基本 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

并在 URL 中使用以下参数:

client_id=backendClient

范围=读取

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>

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

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

谢谢。

【问题讨论】:

  • 请添加有关您的 OAuth2Authenticator 调用的示例,因为您正在询问您做错了什么。 Xamarin.Auth 为我工作 Dropbox 身份验证。对于 Microsoft Live,我必须进行一些修改,因为与 Xamarin.Auth 中实现的请求相比,请求的处理略有不同。还添加一些您正在使用的 Xamarin.Auth 版本的信息。
  • 对我来说,经过一番研究,这很容易理解,可能是什么问题。我的后端 OAuth2 实现需要带有 Authorization 键和散列值的标头。问题是,Xamarin.Auth 无法传递标头 - github.com/xamarin/Xamarin.Auth/issues/61 有什么解决方法吗?
  • 你正在使用这个项目的哪个分支?过时的主分支 oder github.com/xamarin/Xamarin.Auth/tree/portable-bait-and-switch 这似乎也是 Nuget 包的基础(如果我没记错的话是 1.5 版)。
  • 我也有同样的问题。如何使用 OAuth2Authenticator 和用户名和密码而不是 clientid 和 secret ?

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多