CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Windows Live SDK for Windows 8

1235 Views   
namespace MsAccount { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { var _Result = await LoginAsync(true); if (!_Result.HasValue) { // TODO: error occurred } else if (_Result.Value) { this.DataContext = new { Name = User.NameFull, Photo = User.PhotoLocation }; } else { // TODO: login failed } } public async Task<bool?> LoginAsync(bool logout) { try { var _Client = new Microsoft.Live.LiveAuthClient() { Theme = Microsoft.Live.ThemeType.Light }; var can = _Client.CanLogout; if (logout && can) _Client.Logout(); LoginResult = await _Client.LoginAsync(new string[] { "wl.basic", "wl.emails", "wl.skydrive", "wl.photos", "wl.emails", "wl.calendars" }); if (LoginResult.Status != Microsoft.Live.LiveConnectSessionStatus.Connected) return false; Client = new Microsoft.Live.LiveConnectClient(LoginResult.Session); var _MeResult = (await Client.GetAsync("me")).Result; dynamic _PhotoResult = (await Client.GetAsync("me/picture")).Result; var _PhotoLocation = new Uri(_PhotoResult.location, UriKind.Absolute); User = new LiveId(_MeResult, _PhotoLocation); return true; } catch (Exception) { System.Diagnostics.Debugger.Break(); return null; } } public static Microsoft.Live.LiveLoginResult LoginResult { get; private set; } public static Microsoft.Live.LiveConnectClient Client { get; private set; } public static LiveId User { get; private set; } } public class LiveId { // used by Live ID public LiveId(IDictionary<string, object> result) : this(result, null) { } public LiveId(IDictionary<string, object> result, Uri image) { Id = result.ContainsKey("id") ? result["id"] as string : string.Empty; NameFull = result.ContainsKey("name") ? result["name"] as string : string.Empty; NameFirst = result.ContainsKey("first_name") ? result["first_name"] as string : string.Empty; NameLast = result.ContainsKey("last_name") ? result["last_name"] as string : string.Empty; Gender = result.ContainsKey("gender") ? result["gender"] as string : string.Empty; Locale = result.ContainsKey("locale") ? result["locale"] as string : string.Empty; Uri _Uri; if (Uri.TryCreate(result.ContainsKey("link") ? result["link"] as string : string.Empty, UriKind.Absolute, out _Uri)) Link = _Uri; DateTime _Date; if (DateTime.TryParse(result.ContainsKey("updated_time") ? result["updated_time"] as string : string.Empty, out _Date)) UpdatedTime = _Date; var _Emails = result.ContainsKey("emails") ? result["emails"] as IDictionary<string, object> : new Dictionary<string, object>(); EmailPreferred = _Emails.ContainsKey("preferred") ? _Emails["preferred"] as string : string.Empty; EmailAccount = _Emails.ContainsKey("account") ? _Emails["account"] as string : string.Empty; if (image != null) { PhotoLocation = image; var _PhotoBitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage(image); PhotoBitmap = _PhotoBitmap; } } public string Id { get; set; } public string NameFull { get; set; } public string NameFirst { get; set; } public string NameLast { get; set; } public Uri Link { get; set; } public string Gender { get; set; } public string Locale { get; set; } public DateTime UpdatedTime { get; set; } public string EmailAccount { get; set; } public string EmailPreferred { get; set; } public Uri PhotoLocation { get; set; } public Windows.UI.Xaml.Media.Imaging.BitmapImage PhotoBitmap { get; set; } } }
by Jerry Nixon
  November 08, 2012 @ 11:53am
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies