c# - 在 WP7 中存储 LiveConnectSession

标签 c# windows-phone-7

我想在我的 Windows Phone 7 应用程序上使用 Hotmail API。因此,我必须授予对我的移动应用程序的访问权限。我使用 Microsoft Live Connect API 来做到这一点,这对于一开始真的很有效。我使用以下代码进行身份验证:

string[] requiredScope = { "wl.signin", "wl.calendars_update", "wl.offline_access" };
LiveAuthClient auth = new LiveAuthClient(Configuration.ClientID);
auth.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(authInitializeCompletedHandler);
auth.LoginAsync(requiredScope);

ClientID 是我从 MS 获得的用于注册我的应用程序的 ID。我的回调看起来(简单)如下:

private void authInitializeCompletedHandler(object sender, LoginCompletedEventArgs e)
{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        this._session = e.Session; // Session hat AccessToken und RefreshToken
        this._isAuthorized = true;
        callDelegates();
    }
}

如果该调用成功(并且当前成功,所以它有效),我将存储检索到的 session 。这个 session 可以在 LiveConnectClient 构造函数中使用,以启用与 MS API 的通信,在我的应用程序中是 Hotmail API。

LiveConnectClient client = new LiveConnectClient(_session);

我的问题是我想以某种方式保存该 session ,以便当应用程序被逻辑删除并重新获得焦点时,我不希望用户再次请求许可 - 他已经给了我许可。我自己发现返回的 session 包含 AccessToken、RefreshToken 和 Expires 属性(我知道那是什么,这不是问题)。我如何在以后的通话中使用这些信息? Microsoft 的相关文档到此结束,我找不到任何有用的信息。

我找到了 2 个解决方法:

  • Live Connect Button : 不应该这样做,因为我不想在我取回 session 变量时显示实时按钮。
  • RESTful API :这需要我完全重写到目前为止所做的一切,因为一切都依赖于 LiveConnectClient(这使得通信非常容易)。

但我真的不想使用该解决方法。我相信/希望有更好的解决方案。

最佳答案

看起来这个问题的答案如下

  1. Make sure you applications include both wl.signin and wl.offline_access scopes.

  2. If you're not using the signin control provided in the SDK, you should always call LiveAuthClient.InitializeAsync when the app starts. Only call LiveAuthClient.LoginAsync if InitializeAsync does not return a valid session.

These steps will ensure you receiving a refresh token so no need to prompt for login or consent after the first time.

来自: http://social.msdn.microsoft.com/Forums/en-US/messengerconnect/thread/6df1f31d-a22c-469c-b9f4-5c79e0882701

关于c# - 在 WP7 中存储 LiveConnectSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13726042/

相关文章:

c# - 属性名称并需要它的值

c# - 使图像适合 PictureBox

c# - 在 WP7 中绑定(bind) ToggleButton 的 IsChecked 属性

windows-phone-7 - 选择 CollectionViewSource.Filter 上列表框中的第一项

windows - 如果使用环绕面板,则 ScrollIntoView 在列表框中不起作用

c# - 获取 XML 的另一个子元素中的子元素

c# - AutoMapper 并将日期时间转换为字符串

c# - Entity Framework 6 - 处理嵌套对象的加载

c# - 处置的控件是否应该能够安全地忽略事件回调?

windows-phone-7 - 无尽的枢轴控制