OneDrive auto-login after initial authorization

I want the user to be able to automatically log in to my WPF C # application after he / she accepts it and first logs in manually. My code is currently working for logging in using the prompt window:

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 to save some information (maybe?) And use it the next time my application is launched to log in to a specific user without showing this prompt window? I read about the method GetSilentlyAuthenticatedMicrosoftAccountClienton OneDriveClient, but it doesn't seem to be included in Microsoft.OneDrive.SDK 2.0.0 (all samples that use this link and OneDriveClientExtensionsthe SDK link in version 1.1.5). Do you know how to do this?

+4
2

//

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

Source: https://habr.com/ru/post/1651760/


All Articles