2013-09-23 30 views
2

我想讓我的WinForms-App在Microsoft帳戶中使用SingleSign-On(SSO)功能。
我創建了一個LiveApp,我可以使用LiveSDK 5.4登錄到我的應用程序。
但每次我點擊我的登錄按鈕時,出現權限列表,我需要再次接受它。權限屏幕每次出現

這是我的代碼:

private const string ClientID = "{MY_CLIENT_ID}"; 
private LiveAuthClient liveAuthClient; 
private LiveConnectClient liveConnectClient; 
string[] scopes = new string[] { "wl.offline_access", "wl.emails", "wl.signin" }; 

private void buttonLogin_Click(object sender, EventArgs e) 
{ 
    liveAuthClient = new LiveAuthClient(ClientID); 
    webBrowser1.Navigate(liveAuthClient.GetLoginUrl(scopes)); 
} 

private async void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) 
{ 
    if (this.webBrowser1.Url.AbsoluteUri.StartsWith("https://login.live.com/oauth20_desktop.srf")) 
    { 
     AuthResult authResult = new AuthResult(this.webBrowser1.Url); 
     if (authResult.AuthorizeCode != null) 
     { 
      try 
      { 
       LiveConnectSession session = await liveAuthClient.ExchangeAuthCodeAsync(authResult.AuthorizeCode); 
       this.liveConnectClient = new LiveConnectClient(session); 
       LiveOperationResult meRs = await this.liveConnectClient.GetAsync("me"); 
       dynamic meData = meRs.Result; 
       if(string.Equals(meData.emails.account, MyAppUser.EmailAddress)) 
        MessageBox.Show("Successful login: " + meData.name); 
      } 
      catch (LiveAuthException aex) 
      { 
       MessageBox.Show("Failed to retrieve access token. Error: " + aex.Message); 
      } 
      catch (LiveConnectException cex) 
      { 
       MessageBox.Show("Failed to retrieve the user's data. Error: " + cex.Message); 
      } 
     } 
     else 
     { 
      MessageBox.Show(string.Format("Error received. Error: {0} Detail: {1}", authResult.ErrorCode, authResult.ErrorDescription)); 
     } 
    } 
} 

我需要改變什麼?我不希望用戶接受每個登錄的權限。

回答

0

我只在Objective C中使用過這個API,但是我必須遵循兩個步驟。

  1. 使用wl.offline_access作用域。 (您已經在做)
  2. 如果會話對象爲空,則只顯示登錄屏幕。如果您的會話對象已經被填充,您可以像登錄成功時那樣繼續。
2

您可以使用IRefreshTokenHandler保存令牌下面的例子:在那之後

public class RefreshTokenHandler : IRefreshTokenHandler 
{ 
    private string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\oneDrive\\RefreshTokenHandler\\RefreshTokenInfo.RefreshToken-me"; 
    public Task<RefreshTokenInfo> RetrieveRefreshTokenAsync() 
    { 
     return Task.Factory.StartNew<RefreshTokenInfo>(() => 
     { 
      if (File.Exists(path)) 
      { 
       return new RefreshTokenInfo(File.ReadAllText(path)); 
      } 
      return null; 
     }); 
    } 

    public Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo) 
    { 
     // Note: 
     // 1) In order to receive refresh token, wl.offline_access scope is needed. 
     // 2) Alternatively, we can persist the refresh token. 
     return Task.Factory.StartNew(() => 
     { 
      if (File.Exists(path)) File.Delete(path); 
      if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path)); 
      File.AppendAllText(path, tokenInfo.RefreshToken); 
     }); 
    } 
} 

你會得到如下:

RefreshTokenHandler handler = new RefreshTokenHandler(); 
liveAuthClient = new LiveAuthClient(ClientID, handler); 
var Session = liveAuthClient.InitializeAsync(scopes).Result.Session; 
if (Session == null) 
{ 
    webBrowser1.Navigate(liveAuthClient.GetLoginUrl(scopes)); 
} 
else 
{ 
    try 
    { 
     this.liveConnectClient = new LiveConnectClient(Session); 
     LiveOperationResult meRs = await this.liveConnectClient.GetAsync("me"); 
     dynamic meData = meRs.Result; 
     if (string.Equals(meData.emails.account, MyAppUser.EmailAddress)) 
      MessageBox.Show("Successful login: " + meData.name); 
    } 
    catch (LiveAuthException aex) 
    { 
     MessageBox.Show("Failed to retrieve access token. Error: " + aex.Message); 
    } 
    catch (LiveConnectException cex) 
    { 
     MessageBox.Show("Failed to retrieve the user's data. Error: " + cex.Message); 
    } 
}