初始授权后OneDrive自动登录

时间:2016-08-19 09:02:26

标签: c# login onedrive

我希望能够在他/她接受后自动登录我的WPF C#应用程序并首次手动登录。目前我使用提示窗口登录的代码有效:

try
{
    _msaAuthenticationProvider = new MsaAuthenticationProvider("XXXX",
        "https://login.live.com/oauth20_desktop.srf", new[] {"onedrive.readonly", "wl.signin", "wl.offline_access" });
    await _msaAuthenticationProvider.AuthenticateUserAsync();
    _oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", _msaAuthenticationProvider);

    Item item = await _oneDriveClient
        .Drive
        .Root
        .Request()
        .GetAsync();

    Print("Logged in as " + item.CreatedBy.User.DisplayName);
}
catch (Exception exc)
{
    PresentServiceException(exc);
}

现在问题是如何保存一些信息(令牌可能?)并在下次启动我的应用程序时使用它们登录特定用户而不显示提示窗口?我在GetSilentlyAuthenticatedMicrosoftAccountClient上看过OneDriveClient方法,但似乎没有包含在Microsoft.OneDrive.SDK 2.0.0中(所有使用此版本的示例以及OneDriveClientExtensions版本中对SDK的引用1.1.5)。你知道如何做到这一点吗?

2 个答案:

答案 0 :(得分:5)

//你的代码

_msaAuthenticationProvider = new MsaAuthenticationProvider("XXXX", "https://login.live.com/oauth20_desktop.srf", new[] {"onedrive.readonly", "wl.signin", "wl.offline_access" });
await _msaAuthenticationProvider.AuthenticateUserAsync();
_oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", _msaAuthenticationProvider);
await _msaAuthenticationProvider.AuthenticateUserAsync();

//添加此
//保存刷新令牌

var refreshtoken = (((MsaAuthenticationProvider)oneDriveClient.AuthenticationProvider).CurrentAccountSession).RefreshToken;

//在会话之间保存此刷新令牌的安全性  // ------------------------------------
//稍后,如果要连接到OneDrive,请创建AccountSession并使用存储的RefreshToken

AccountSession session = new AccountSession();
session.ClientId = <<your id>>; // your "XXXX"
session.RefreshToken = refreshtoken;
_msaAuthenticationProvider = new MsaAuthenticationProvider(....
_oneDriveClient = new OneDriveClient(....
_msaAuthenticationProvider.CurrentAccountSession = session;
await _msaAuthenticationProvider.AuthenticateUserAsync();

答案 1 :(得分:0)

确实,这在SDK的早期版本中可用。当它被重构为v2时,并没有重新实现所有的身份验证机制。我打开了一个Github问题来解决这个问题。 https://github.com/OneDrive/onedrive-sdk-dotnet-msa-auth-adapter/issues/7