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

Ste*_*kes 7 c# liveconnect microsoft-metro windows-8 live-connect-sdk

我正在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
Run Code Online (Sandbox Code Playgroud)

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

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

例外:

Cannot sign out from the application since the user account is connected. (Exception from HRESULT: 0x8086000E)
Run Code Online (Sandbox Code Playgroud)

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

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

Ste*_*kes 5

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

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

Windows.Security.Authentication.OnlineId.OnlineAuthenticator.CanSignOut
Run Code Online (Sandbox Code Playgroud)

代码示例:

    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;
    }
Run Code Online (Sandbox Code Playgroud)