初始授权后,OneDrive自动登录 [英] OneDrive auto login after initial authorisation

查看:303
本文介绍了初始授权后,OneDrive自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在用户接受我的WPF C#应用程序后自动登录并首次手动登录.目前,我使用提示窗口登录的代码有效:

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

现在的问题是,如何保存一些信息(可能是令牌?)并在下次我的应用启动时使用它们来登录特定用户而不显示该提示窗口?我已经阅读了有关OneDriveClient上的GetSilentlyAuthenticatedMicrosoftAccountClient方法的信息,但似乎未包含在Microsoft.OneDrive.SDK 2.0.0中(所有使用此方法和OneDriveClientExtensions的示例都引用了版本1.1.5中的SDK).你知道如何做到这一点吗?

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?

推荐答案

//您的代码

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

//在会话之间安全地存储此刷新令牌.
//------------------------------------
//稍后,如果您想连接到OneDrive,请创建AccountSession并使用存储的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();

这篇关于初始授权后,OneDrive自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆