初始授权后OneDrive自动登录

twe*_*ant 4 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);
}
Run Code Online (Sandbox Code Playgroud)

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

小智 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();
Run Code Online (Sandbox Code Playgroud)

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

var refreshtoken = (((MsaAuthenticationProvider)oneDriveClient.AuthenticationProvider).CurrentAccountSession).RefreshToken;
Run Code Online (Sandbox Code Playgroud)

//在会话之间存储此刷新令牌.
// ------------------------------------
//稍后,如果要连接到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();
Run Code Online (Sandbox Code Playgroud)

  • 你应该解释一下你的解决方案. (2认同)