简体   繁体   English

初始授权后OneDrive自动登录

[英]OneDrive auto login after initial authorisation

I want to be able login user automatically in my WPF C# app after he/she accepts it and login manually for the first time. 我想能够在他/她接受后自动登录我的WPF C#应用程序并首次手动登录。 Currently my code to login using prompt window works: 目前我使用提示窗口登录的代码有效:

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);
}

Now the question is how do I save some info (tokens maybe?) and use them next time my app is launched to log in a specific user without showing that prompt window? 现在的问题是如何保存一些信息(令牌可能?)并在下次启动我的应用程序时使用它们登录特定用户而不显示提示窗口? I've read about GetSilentlyAuthenticatedMicrosoftAccountClient method on OneDriveClient but it seems not to be included in Microsoft.OneDrive.SDK 2.0.0 (all samples that use this and OneDriveClientExtensions reference to SDK in version 1.1.5). 我在OneDriveClient上阅读了有关GetSilentlyAuthenticatedMicrosoftAccountClient方法的内容,但它似乎没有包含在Microsoft.OneDrive.SDK 2.0.0中(所有使用此示例的示例和OneDriveClientExtensions引用版本1.1.5中的SDK)。 Do you have any idea how to accomplish that? 你知道怎么做到这一点吗?

// your code //你的代码

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

// add this //添加这个
// save refresh token //保存刷新令牌

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

// store this refresh token secure between sessions. //在会话之间存储此刷新令牌。
// ------------------------------------ // ------------------------------------
// later, if you want to connect to OneDrive, create AccountSession and use that stored 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();

Indeed this was available in the previous version of the SDK. 实际上,这在SDK的早期版本中可用。 When it was refactored for v2, not quite all of the authentication mechanisms were re-implemented. 当它被重构为v2时,并没有重新实现所有的身份验证机制。 I have opened a Github issue to deal with this. 我打开了一个Github问题来解决这个问题。 https://github.com/OneDrive/onedrive-sdk-dotnet-msa-auth-adapter/issues/7 https://github.com/OneDrive/onedrive-sdk-dotnet-msa-auth-adapter/issues/7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM