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. 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). 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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With