3

在 Xamarin.Forms 项目中,我想使用 Xamarin.Auth 来安全地存储和检索 Account 对象。请注意,我不会考虑与 Facebook、Google 或任何其他提供端到端 oAuth 登录体验的服务集成。

4

3 回答 3

2

如果您只是在安全存储对象之后,那么 XLabs.Platform 具有 ISecureStorage 接口,该接口适用于 iOS、Android 和 WP。如果您正在使用 Forms,我建议您从 NuGet 安装 XLabs.Forms 包,然后使用 Ioc (XLabs.Ioc) 将实现注入 PCL 代码。您还可以使用 XLabs.Serialization 使用 Json.NET、ServiceStack 或其他序列化器(如 ProtoBuffer(可作为 XLabs.Serialization 插件提供)使用 XLabs.Serialization 来序列化/反序列化字节。

https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Platform/XLabs.Platform/Services/ISecureStorage.cs

https://www.nuget.org/packages?q=XLabs

于 2015-02-25T20:54:12.040 回答
1

Xamarin 论坛正在讨论此主题。您可能想查看那里的线程

Xamarin.Auth 不是特定于 PCL 和平台的。您需要使用自定义渲染器。首先为您的登录创建一个表单页面:

public class LoginPage : ContentPage
{
}

然后为平台实现它,例如 iOS:

[assembly: ExportRenderer (typeof (LoginPage), typeof (LoginPageRenderer))]

namespace Demo.XForms.iOS
{
    public class LoginPageRenderer : PageRenderer
    {
        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);

            var auth = new OAuth2Authenticator (
                clientId: ...,
                scope: "basic",
                authorizeUrl: new Uri ("..."),
                redirectUrl: new Uri ("..."));

            auth.Completed += (sender, eventArgs) => {
                DismissViewController (true, null);
                if (eventArgs.IsAuthenticated) {
                    App.SaveToken(eventArgs.Account.Properties["access_token"]);
                } else {
                    // The user cancelled
                }
            };

            PresentViewController (auth.GetUI (), true, null);
        }
    }
}

有了它,您可以在 Forms 中使用它:

this.MainPage = new LoginPage ();
于 2015-02-25T15:18:19.503 回答
1

在 2019 年,您可以使用Xamarin.Essentials中的SecureStorage来存储帐户信息。在底层,它使用平台标准:iOS 上的 Keychain,Android 上的 Keystore。因此它也支持备份到云端,但也需要权限。

https://docs.microsoft.com/en-us/xamarin/essentials/secure-storage

此外,现在不推荐使用 Xamarin.Auth 中的 AccountStore,他们建议现在使用 Xamarin.Essentials 中的 SecureStorage。

于 2019-07-23T07:06:42.870 回答