2014-10-22 51 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框架,但我会假设消息和依赖注入会有类似的功能。

相关问题