2014-10-22 50 views
0

目前我正在試圖給手機樣品應用從官方ADAL github repo轉換爲caliburn.micro MVVM應用程序。但是有太多的移動部件支持到代碼隱藏中以與WebAuthenticationBroker相處,我現在不知道如何在代理完成登錄後再次激活應用時如何將其推入視圖模型並正確處理導航。由於目前我完全無能爲力,因此目前還沒有代碼可供分享。我在我的Windows Phone 8.1應用程序使用了與MvvmLight沿ADALActive Directory驗證庫(ADAL)在MVVM手機8.1應用

回答

0

。你需要做的是一旦你得到一個令牌,你需要使用Messenger模式發送消息。所有需要令牌並且已經訂閱的視圖模型都會收到它。這是我在我的應用程序中使用MvvmLight所做的事情。請記住,您需要有一個ContinuationManager類和IWebContinuable接口才能使應用程序正常工作。

private async void Login() 
    { 
     AuthenticationResult result = null; 
     context = AuthenticationContext.CreateAsync("https://login.windows.net/<tenant-id>").GetResults(); 
     try 
     { 
    //try to check if you can get the token without showing pop-up     
     result = await context.AcquireTokenSilentAsync("https://management.core.windows.net/", "<clientid>"); 
      if(result.Status==AuthenticationStatus.ClientError) 
      { 
       bool exists = CheckInVault(); 
       if(exists) 
       { 
        PasswordVault vault = new PasswordVault(); 
        var tokenvault = vault.FindAllByResource("Token"); 
        string RefreshToken = tokenvault[0].Password; 
        var refresh=await context.AcquireTokenByRefreshTokenAsync(RefreshToken, clientid); 
        vault.Remove(tokenvault[0]); 
        StoreToken(refresh); 
       } 
       else 
       { 

        context.AcquireTokenAndContinue("https://management.core.windows.net/", clientid, WebAuthenticationBroker.GetCurrentApplicationCallbackUri(), StoreToken); 
       } 

      } 
      else if(result != null && result.Status == AuthenticationStatus.Success) 
      { 
       // A token was successfully retrieved. Post the new To Do item 
       bool exists = CheckInVault(); 
       if (exists) 
       { 
        PasswordVault vault = new PasswordVault(); 
        var tokenvault = vault.FindAllByResource("Token"); 
        vault.Remove(tokenvault[0]); 

       } 
       StoreToken(result); 
      } 
      //this method will be called when app is opened first time and pop-up appears  
    result=await context.AcquireTokenSilentAsync("https://management.core.windows.net/", "<client-id>"); 
     } 
     catch(Exception e) 
     { 
      MessageDialog dialog = new MessageDialog("Error"); 
     } 
    } 

我在做什麼這裏是 - 獲得訪問令牌和參考令牌當用戶註冊首先,我存儲刷新令牌在PasswordVault後,以得到它在未來啓用單點登錄-上。 ADAL實際上使用它的緩存功能,但有時單點登錄失敗了,因此使用PasswordVault來存儲刷新令牌。驗證完成後,我有一個委託給StoreToken函數,我實際上存儲新的刷新令牌並將訪問令牌發送給所有使用MvvmLight中的Messenger類的訂戶。

private void StoreToken(AuthenticationResult result) 
    { 
     try 
     { 
      var token = result.AccessToken; 
      Messenger.Default.Send<string>(token); //send the access token. 
      PasswordVault vault = new PasswordVault(); 
      PasswordCredential credential = new PasswordCredential(); 
      credential.UserName = result.AccessToken; 
      credential.Password = result.RefreshToken; 
      credential.Resource = "Token"; 
      vault.Add(credential); 
     } 
     catch(Exception e) 
     { 

     } 
    } 

我建議在視圖模型中處理導航。定義一個輔助類喜歡的NavigationService:

public class NavigationService:INavigationService 
{ 
    private Frame _frame; 
    public Frame Frame 
    { 
     get 
     { 
      return _frame; 
     } 
     set 
     { 
      _frame = value; 
      _frame.Navigated+= OnFrameNavigated; 
     } 
    } 


    public void NavigateTo(string type) 
    { 
     Frame.Navigate(Type.GetType(type)); 
    } 


    public void GoForward() 
    { 
     if (Frame.CanGoForward) 
      Frame.GoForward(); 
    } 

    public void GoBack() 
    { 
     if (Frame.CanGoBack) 
      Frame.GoBack(); 
    } 
} 

從視圖模型導航到一個頁面,您使用的NavigateTo(string)方法作爲

 NavigateTo("<fully qualified class name of the page you want to navigate to>,<assembly name>") 

我也建議使用IoC容器(MvvmLight爲您提供ViewModelLocator類),以便您可以維護視圖模型和助手(如NavigationService)的單例實例。我還沒有使用過CaliburnMicro框架,但我會假設消息和依賴注入會有類似的功能。

相關問題