簡體   English   中英

使用Caliburn.Micro在Windows Phone上進行Facebook身份驗證回調

[英]Facebook authentication callback on Windows Phone using Caliburn.Micro

我正在借助Caliburn.Micro框架開發Windows Phone應用。 我正在嘗試使用本教程實現Facebook登錄。

ContinuationManager導致我遇到問題,因為它假定不使用MVVM模型,而是將整個代碼保留在視圖的后面。 有沒有一種干凈的方法來重現控件並將WebAuthenticationBrokerContinuationEventArgs傳遞給LogInViewModel (而不是LogInView ),以便身份驗證過程可以繼續?

換句話說,如何在用戶成功完成Facebook登錄過程后在LogInViewModel調用public async void ContinueWithWebAuthenticationBroker( WebAuthenticationBrokerContinuationEventArgs args)方法?

有幾種方法可以使這個工作,讓我給你我的...

我需要與第3 IWebAuthenticationContinuable OAuth連接,並且為了與該方進行所有通信,我創建了一個Service class -該類實現了IWebAuthenticationContinuable接口。 換句話說,所有API調用都在其中,還有ContinueWebAuthentication方法。 在ContinueWebAuthentication方法內部,我調用一個Status事件-在Service類上也聲明了該事件,以將OAuth處理的實際結果狀態通知給所有偵聽器。

因此,剩下要做的就是在ViewModel中注冊該事件,您將在該事件中啟動OAuth流程的開始並根據狀態更改進行操作,以驗證OAuth流程是否正常。 通過觸發一個方法(在我的服務類別中為GetAccessToken()方法)來啟動實際的OAuth流程,然后在此方法中啟動WebuthenticationBroker.AuthenticateAndContinue()方法。

其他解決方案是使用MVVM消息傳遞而不是真實事件。 但這僅僅是語義。

我正在使用Caliburn,並且最近實現了Facebook登錄。

在我的LoginViewModel中,我調用WebAuthenticationBroker.AuthenticateAndContinue(url); 啟動登錄。

在App.xaml.cs中,我重寫OnActivated方法,檢查參數是否來自身份驗證代理,如果是,則將其作為消息發送。

protected override void OnActivated(IActivatedEventArgs args)
{            
    base.OnActivated(args);

    switch (args.Kind)
    {
        case ActivationKind.WebAuthenticationBrokerContinuation:
                OnWebAuthenticationBrokerContinuation((WebAuthenticationBrokerContinuationEventArgs)args);
                break;
        }
}

private void OnWebAuthenticationBrokerContinuation(WebAuthenticationBrokerContinuationEventArgs args)
{
    var eventAggregator = container.GetInstance<IEventAggregator>()
    eventAggregator.PublishOnUIThread(args);
}

我的LoginViewModel實現了IHandle<WebAuthenticationBrokerContinuationEventArgs>並在Handle(WebAuthenticationBrokerContinuationEventArgs message)獲取Facebook令牌並使用該服務進行實際登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM