Windows 8 深度整合 Microsoft 帳戶(原 Windows Live ID),應用程式可在使用者同意下存取基本資料、頭像、甚至透過 Live Connect API 呼叫雲端服務(OneDrive、Outlook、行事曆等)。本文涵蓋三大主題:使用者資訊讀取、帳戶圖片/視訊管理、OAuth 驗證流程。
if (UserInformation.NameAccessAllowed) {
string displayName = await UserInformation.GetDisplayNameAsync();
string firstName = await UserInformation.GetFirstNameAsync();
string lastName = await UserInformation.GetLastNameAsync();
// 以下需 Package.appxmanifest 加入
// string domain = await UserInformation.GetDomainNameAsync();
// string principal = await UserInformation.GetPrincipalNameAsync();
}
所有 API 均受 NameAccessAllowed 守護,使用者可在系統設定隨時撤銷授權,應用程式需妥善處理拒絕存取的情境。
StorageFile small = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile;
StorageFile large = UserInformation.GetAccountPicture(AccountPictureKind.LargeImage) as StorageFile;
StorageFile video = UserInformation.GetAccountPicture(AccountPictureKind.Video) as StorageFile;
// 使用 OpenReadAsync 串流載入至 Image/MediaElement
FileOpenPicker picker = new FileOpenPicker { ... };
StorageFile file = await picker.PickSingleFileAsync();
if (file != null) {
// 可分別指定小圖、大圖、視訊,null 表示保持原值
SetAccountPictureResult result = await UserInformation.SetAccountPicturesAsync(null, file, null);
if (result == SetAccountPictureResult.Success) { /* 成功 */ }
}
UserInformation.AccountPictureChanged += (sender, e) => {
// 重新載入三種圖片/視訊
};
https://webabcd.cnblogs.com)OnlineIdAuthenticator authenticator = new OnlineIdAuthenticator();
var scopes = new List {
new OnlineIdServiceTicketRequest("wl.basic wl.contacts_photos wl.calendars", "DELEGATION")
};
var result = await authenticator.AuthenticateUserAsync(scopes, CredentialPromptType.PromptIfNeeded);
if (result.Tickets[0].Value != string.Empty) {
string token = result.Tickets[0].Value; // 可用於 REST API 呼叫
// 例如:https://apis.live.net/v5.0/me?access_token={token}
} else {
int errorCode = result.Tickets[0].ErrorCode;
}
await authenticator.SignOutUserAsync();
| Scope | 權限說明 |
|---|---|
| wl.basic | 基本個人資料(姓名、性別、地區等) |
| wl.emails | 主要電子郵件 |
| wl.contacts_photos | 聯絡人相片 |
| wl.calendars | 行事曆讀寫 |
| wl.photos | 相簿存取 |
| wl.skydrive | OneDrive 檔案存取 |
Microsoft 帳戶整合提供無縫的單一登入體驗,配合 Live Connect 豐富的 API 生態系,應用程式能輕鬆實作社交分享、檔案同步、行事曆同步等進階功能。開發者需注意隱私權設定與權限範圍的最小化原則。