mvvmcross 和身份验证

标签 mvvmcross azure-mobile-services

有没有办法在 mvvmcross 框架中通过 Facebook 对用户进行身份验证?我目前正在尝试使用移动 Azure 服务向 Facebook 进行身份验证,但没有成功。在不使用 mvvmcross 的情况下,我可以很好地进行身份验证。

谢谢! 标记

最佳答案

在 MVVM 的意义上,我发现不,你不能。 Facebook 登录页面上的属性不可绑定(bind),也不应该并且最好将其视为不受您控制的模态视图

我要做的是将其设为 View 关注点并使用 Xamarin.Auth 进行身份验证。

例如,假设您有一个 LoginView 和 LoginViewModel。

LoginView 提供您的标准登录电子邮件/密码,但有一个选项(按钮)通过 facebook 进行身份验证

从该 View 连接到 facebook 登录按钮的 touchupinside 事件

this.btnFacebookLogin.TouchUpInside += (object sender, EventArgs e) =>
{
    DoFacebookLogin ();
}

然后在您的 DoFacebookLogin 方法中“呈现”facebook 的 View Controller ,如此处所述https://github.com/xamarin/Xamarin.Auth/blob/master/GettingStarted.md

例如:

private void DoFacebookLogin ()
{
    var auth = new OAuth2Authenticator (
        clientId: "yournumericclientidhere",
        scope: "",
        authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"),
        redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html"));

        auth.AllowCancel = true;

        auth.Completed += (sender, eventArgs) => {
            DismissViewController (false, null);
            if (eventArgs.IsAuthenticated) {
                string user = eventArgs.Account.Serialize ();
                var messenger = Mvx.Resolve<IMvxMessenger> ();
                messenger.Publish (new FacebookLoggedIn (user));
            } else {
                // Cancelled here
            }
        };

        var vc = auth.GetUI ();
        this.PresentViewController (vc, true, null);
    }

不需要处理取消,因为模态视图 Controller 会带你回到你的登录 View

成功关闭 viewcontroller 然后我将使用 mvx 对 eventaggregator (plugins.messenger) 的解释向 viewmodel 发送一条消息,告知 facebook 模态视图已关闭,您可以通过该消息传递帐户详细信息 - accesstoken 等回到 View 模型,按照您的意愿进行操作。

View (如上):

string user = eventArgs.Account.Serialize();
var messenger = Mvx.Resolve<IMvxMessenger> ();
messenger.Publish (new FacebookLoggedIn (user));

PCL 中的消息类:

public class FacebookLoggedIn : MvxMessage
{
    public FacebookLoggedIn(object sender) : base(sender) {}
}

ViewModel 也在你的 PCL 中:

public LoginViewModel()
{
    var messenger = Mvx.Resolve<IMvxMessenger> ();
    user = messenger.SubscribeOnMainThread<FacebookLoggedIn> (OnFacebookLoggedIn);
}

private void OnFacebookLoggedIn (FacebookLoggedIn MvxMessage)
{
    ... do something with the accesstoken? call your IUserService etc
    ShowViewModel<MainViewModel> ();
}

由于您关闭了 facebook viewcontroller,您会发现自己在自动导航到 MainView 之前暂时回到登录 View

在您的 View 项目中,您需要确保插件已加载,否则您将在构建 View 模型期间收到错误,因此在 setup.cs 中

protected override void InitializeLastChance ()
{
    Cirrious.MvvmCross.Plugins.Messenger.PluginLoader.Instance.EnsureLoaded();

    base.InitializeLastChance ();
 }

此外,您还可以在本地存储帐户凭据,这在 AccountStore.Create().Save 下的 Xamarin.Auth 链接中有描述。请注意,如果您收到平台不支持的异常,请将 PLATFORM_IOS 作为预处理器指令添加到您的项目中。

我意识到这个问题已经有几个月了,但是因为它在谷歌上的评分很高,所以我想我会提供一个答案,因为没有答案

关于mvvmcross 和身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062882/

相关文章:

c# - 如何为使用 Code First 的应用程序的 SQL View 创建 Controller ?

具有现有数据库的 Azure 移动服务(无需架构更改)

azure-mobile-services - 在 Windows Azure 移动服务中实现自定义身份验证

javascript - 在 Azure 中处理多个异步请求?

c# - Android View 状态 : pressed, 已激活、已选择等绑定(bind)。我必须编写自定义绑定(bind)吗?

ios - 如何使用 MvvmCross 和 Xamarin.iOS 将 MvxTableViewSource 绑定(bind)到动态创建的 ViewModel

windows-phone-7 - MvvmCross vnext : CheckBox CheckedChange event to a command with monodroid

ios - 如何使用 WindowsAzureMobileServices.framework 修复 iOS 项目中的重复符号错误

ios - Xamarin iOS UIButton 隐藏/显示

c# - 'DateTime' 不包含 'FromOADate' 的定义