加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Metro App – 如何检测是否使用Live ID或本地帐户登录

发布时间:2020-12-15 08:04:42 所属栏目:百科 来源:网络整理
导读:我正在Live Connect SDK(http://msdn.microsoft.com/en-us/live/default)上构建Metro C#SkyDrive API – 在 Windows 8中,用户可以选择登录到Windows 8计算机使用LOCAL帐户或LIVE帐户. 使用Live Connect SDK时,如果我打电话 // assume wlscopes is properly s
我正在Live Connect SDK(http://msdn.microsoft.com/en-us/live/default)上构建Metro C#SkyDrive API – 在 Windows 8中,用户可以选择登录到Windows 8计算机使用LOCAL帐户或LIVE帐户.

使用Live Connect SDK时,如果我打电话

// assume wlscopes is properly set

LiveAuthClient liveAuthClient = new LiveAuthClient();
LiveLoginResult loginResult = await liveAuthClient.LoginAsync(wlscopes);

// do some stuff on skydrive

liveAuthClient.Logout();   // <-- issue only with live account,not local

当使用LOCAL帐户时,它会让我退出(很棒)

当我在使用LIVE帐户时调用相同的代码时,我得到一个无法处理的异常 – 我甚至无法在此错误周围添加try {} catch {}.

例外:

Cannot sign out from the application since the user account is connected. (Exception from HRESULT: 0x8086000E)

显然,由于在Live帐户下登录的用户无法注销,我的api需要检测当前用户是否正在使用真实帐户,因此我可以阻止调用logout()方法.

所以……我的问题是,如何知道用户在Windows 8中使用哪种帐户类型?

解决方法

找到答案:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.authentication.onlineid.onlineidauthenticator.cansignout.aspx#Y0

以下是我们需要使用的属性:

Windows.Security.Authentication.OnlineId.OnlineAuthenticator.CanSignOut

代码示例:

public async Task<bool> Logout()
    {
        // Check to see if the user can sign out (Live account or Local account)
        var onlineIdAuthenticator = new OnlineIdAuthenticator();
        var serviceTicketRequest = new OnlineIdServiceTicketRequest("wl.basic","DELEGATION");
        await onlineIdAuthenticator.AuthenticateUserAsync(serviceTicketRequest);

        if (onlineIdAuthenticator.CanSignOut)
        {
            LiveAuthClient.Logout();               
        }

        return true;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读