我正在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
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; }