我希望能够在他/她接受并首次手动登录后在我的 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 ?)并在下次启动我的应用程序时使用它们来登录特定用户而不显示提示窗口?我读过有关 OneDriveClient
的 GetSilentlyAuthenticatedMicrosoftAccountClient
方法,但它似乎未包含在 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/