c# - OneDrive 初始授权后自动登录

标签 c# authentication 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);
}

现在的问题是我如何保存一些信息(也许是 token ?)并在下次启动我的应用程序时使用它们来登录特定用户而不显示提示窗口?我读过有关 OneDriveClientGetSilentlyAuthenticatedMicrosoftAccountClient 方法,但它似乎未包含在 Microsoft.OneDrive.SDK 2.0.0 中(所有使用此方法和 OneDriveClientExtensions 的示例 引用 1.1.5 版本的 SDK)。您知道如何实现吗?

最佳答案

//你的代码

_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();

//添加这个
//保存刷新 token

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

//在 session 之间安全地存储此刷新 token 。
//----------------------------------
//稍后,如果你想连接到 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();

关于c# - OneDrive 初始授权后自动登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035054/

相关文章:

c# - 如何在我的 asp.net 项目中按日期查找 TFS 标签 (Visual Studio Team System 2008)

c# - 应用层数格式asp.net mvc

jquery - 如何在另一个 ajax 调用后运行一个 ajax 调用?

windows-phone-7 - SkyDrive 的编程访问

c# - C# 中的多媒体键盘的多媒体控制

c# - 更改数据表列日期格式

c# - 使用引用 token 进行 ASPNETCore SignalR 身份验证

Angular 登录页面

java - OneDrive API 部分下载

javascript - 我可以使用 SkyDrive 托管和链接到 JavaScript 和 CSS 文件吗?