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

Nam*_*mek 5 c# oauth-2.0 xamarin xamarin.auth xamarin.forms

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

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

授权,基本xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

并在URL中使用以下参数:

CLIENT_ID = backendClient

范围=读

grant_type =密码

用户名=管理员

密码=管理员

所以它就像:http: //example.com/backend/oauth/token?client_id = backEndClient&screen = 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"
}
Run Code Online (Sandbox Code Playgroud)

我想通过移动应用程序(使用Xamarin.Forms应用程序)对我的后端服务进行身份验证,并且我尝试使用Xamarin.Auth对我的后端进行身份验证 - https://developer.xamarin.com/guides/xamarin表单/网络服务/认证/ OAuth的/

确实使用OAuth2Authenticator,它至少能够"ping"我的后端(通过放置URL,clientIde等),因为它返回:

<UnauthorizedException>
    <error>unauthorized</error>
    <error_description>Full authentication is required to access this resource</error_description>
</UnauthorizedException>
Run Code Online (Sandbox Code Playgroud)

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

有谁有想法,我怎么能这样做?或者我应该使用别的东西 - 不是Xamarin.Auth?

谢谢.

小智 1

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

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenGoesHere);
Run Code Online (Sandbox Code Playgroud)

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