1

我有一个Xamarin.Android项目和一个ASP.NET MVC 5网站,ASP.NET Identity and OAuth用于帐户管理和登录。

我已使用 Visual Studio 中的 Xamarin 组件添加Xamarin.Auth到项目中。Android

var auth = new OAuth2Authenticator(
    clientId: "client id here",
    scope: "scope here",
    authorizeUrl: new Uri(Constants.OAuthAuthorizeUrl),
    redirectUrl: new Uri(Constants.OAuthRedirectUrl)
)
{
    AllowCancel = false
};

这是在StartActivity.

现在Completed事件处理程序:

auth.Completed += async (sender, args) =>
{
    if (!args.IsAuthenticated)
    {
        RunOnUiThread(() =>
        {
            var newIntent = new Intent(this, typeof(LoginActivity));

            StartActivity(newIntent);
        });
    }

    var parameters = new Dictionary<string, string> { { "ts", DateTime.Now.ToString("ss") } };

    var request = new OAuth2Request("POST", new Uri(Constants.GetCurrentUser), parameters, args.Account);
    var response = await request.GetResponseAsync();
    var responseText = response.GetResponseText();
}

然后我启动身份验证器:

var intent = auth.GetUI(this);
StartActivity(intent);

登录页面按预期显示,我输入凭据并Completed触发事件,并且 AuthenticatorIsAuthenticated值为true,如预期的那样。

当我使用args.Account(用户名为空且我不知道为什么但设置了令牌)发出请求时,ASP.NET 网站无法进行身份验证,我不知道为什么。主体的身份未经过身份验证,但当身份验证器调用重定向 url 时,主体已通过身份验证。

Swift用于OAuth2Swift身份验证的 iOS 应用程序OAuth运行良好。

4

0 回答 0