使用缓存Cognito身份当我第一次登录到我的应用程序,我通过下面的代码:从Xamarin
auth = new Xamarin.Auth.OAuth2Authenticator(
"my-google-client-id.apps.googleusercontent.com",
string.Empty,
"openid",
new System.Uri("https://accounts.google.com/o/oauth2/v2/auth"),
new System.Uri("com.enigmadream.storyvoque:/oauth2redirect"),
new System.Uri("https://www.googleapis.com/oauth2/v4/token"),
isUsingNativeUI: true);
auth.Completed += Auth_Completed;
StartActivity(auth.GetUI(this));
触发此活动:
[Activity(Label = "GoodleAuthInterceptor")]
[IntentFilter(actions: new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataSchemes = new[] { "com.enigmadream.storyvoque" }, DataPaths = new[] { "/oauth2redirect" })]
public class GoodleAuthInterceptor : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Android.Net.Uri uri_android = Intent.Data;
Uri uri_netfx = new Uri(uri_android.ToString());
MainActivity.auth?.OnPageLoading(uri_netfx);
Finish();
}
}
最后这个代码链接注册到Cognito:
private void Auth_Completed(object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs e)
{
if (e.IsAuthenticated)
{
var idToken = e.Account.Properties["id_token"];
credentials.AddLogin("accounts.google.com", idToken);
AmazonCognitoIdentityClient cli = new AmazonCognitoIdentityClient(credentials, RegionEndpoint.USEast2);
var req = new Amazon.CognitoIdentity.Model.GetIdRequest();
req.Logins.Add("accounts.google.com", idToken);
req.IdentityPoolId = "us-east-2:79ebf8e1-97de-4d1c-959a-xxxxxxxxxxxx";
cli.GetIdAsync(req).ContinueWith((task) =>
{
if ((task.Status == TaskStatus.RanToCompletion) && (task.Result != null))
{
ShowMessage(string.Format("Identity {0} retrieved", task.Result.IdentityId));
}
else
ShowMessage(task.Exception.InnerException != null ? task.Exception.InnerException.Message : task.Exception.Message);
});
}
else
ShowMessage("Login cancelled");
}
这一切都很好,登录后,我可以使用我的身份/证书从DynamoDB检索数据。有了这个对象:
Amazon.DynamoDBv2.AmazonDynamoDBClient ddbc = new Amazon.DynamoDBv2.AmazonDynamoDBClient(credentials, RegionEndpoint.USEast2);
第二次,我跑我的应用程序,运行此代码:
if (!string.IsNullOrEmpty(credentials.GetCachedIdentityId()) || credentials.CurrentLoginProviders.Length > 0)
{
if (!bDidLogin)
{
var idToken = credentials.GetIdentityId();
ShowMessage(string.Format("I still remember you're {0} ", idToken));
如果我尝试使用DynamoDB使用的凭据(或任何东西,我假设),在这一点上,我收到我无法访问身份的错误。我必须注销(credentials.Clear()
)并再次登录以获取正确的凭据。 我可以要求用户在每次运行应用程序时都要经历整个登录过程,但这真的很痛苦,因为Google登录过程要求用户知道如何在认证后手动关闭Web浏览器以返回应用程序。有什么我错过了缓存凭据的目的和用法?当我使用大多数应用程序时,他们不要求我每次都登录到我的Google帐户,并关闭网络浏览器以访问其服务器资源。
1)在oauth2登录完成后,您可以使用基于LaunchMode.SingleTask的Activity来关闭Chrome(或Firefox,Samsung等)自定义选项卡,方法是在同一活动中启动OAuth2Authenticator,而不是“MainActivity”(Xamarin.Auth文档(readme.md)对于它们如何显示你的设置是错误的)。 2)你是说第一次登录后,缓存的凭证是空的还是令牌丢失? – SushiHangover
@SushiHangover我相信这些凭据是存在的,因为它告诉我我的ID,并告诉我如果我尝试登录到另一个Google帐户(添加具有不同ID的另一个登录名),则不能使用其他身份。但是,如果我尝试使用凭据,则表示我无法访问身份。我对此非常陌生,所以我不确定这些部分如何组合在一起,或者这些错误是什么意思。 – BlueMonkMN
@SushiHangover我能够获得'LaunchMode.SingleTask'来改善行为,但我仍然不清楚缓存凭据的性质或目的,以及每次运行应用程序时是否需要提示输入Google帐户。它记住我的身份标记,但显然不需要通过身份验证。 – BlueMonkMN