2012-04-24 35 views
15

Có cách nào để đăng nhập vào Live cho một ứng dụng (Silverlight, WP7 có thể) mà không cần phải nhấp vào nút Đăng nhập.SDK trực tiếp - Thử đăng nhập mà không cần SignInButton

Tôi muốn đăng nhập cho tôi động, ví dụ: khi bạn khởi động ứng dụng, tôi muốn đăng nhập vào tôi. Làm thế nào để làm điều này mà không cần đến nút?

Trả lời

29

tôi đã tìm ra cách để làm, vì vậy tôi quyết định chia sẻ:

using System.Windows; 
using Microsoft.Live; 

public class LiveLogin 
{ 

    private static readonly string[] scopes = 
     new string[] { 
      "wl.signin", 
      "wl.basic", 
      "wl.calendars", 
      "wl.calendars_update", 
      "wl.contacts_calendars", 
      "wl.events_create" }; 

    private LiveAuthClient authClient; 
    private LiveConnectClient liveClient; 


    public LiveLogin() 
    { 
     this.authClient = new LiveAuthClient("**your client id here**"); 
     this.authClient.InitializeCompleted += authClient_InitializeCompleted; 
     this.authClient.InitializeAsync(scopes); 
    } 

    private void authClient_InitializeCompleted(object sender, LoginCompletedEventArgs e) 
    { 
     if (e.Status == LiveConnectSessionStatus.Connected) 
     { 
      this.liveClient = new LiveConnectClient(e.Session); 
     } 
     else 
     { 
      this.authClient.LoginCompleted += authClient_LoginCompleted; 
      this.authClient.LoginAsync(scopes); 
     } 
    } 

    private void authClient_LoginCompleted(object sender, LoginCompletedEventArgs e) 
    { 
     if (e.Status == LiveConnectSessionStatus.Connected) 
     { 
      this.liveClient = new LiveConnectClient(e.Session); 
      MessageBox.Show("Signed"); 
     } 
     else 
     { 
      MessageBox.Show("Failed!"); 
     } 
    } 
} 
+0

tín dụng cho: http://social.msdn.microsoft.com/Forums/en-GB/messengerconnect/thread/ a1ae8e9c-47a5-4bd8-b821-33dc1a0d6e94 – Richard

+2

Cảm ơn. Microsoft nên ghi lại nó. – ashraf

+0

Cảm ơn rất nhiều vì đoạn mã ngắn và ngọt ngào này. Tôi đã không nhận được một ý tưởng rõ ràng về cách sử dụng LiveAuthClient. Tài liệu MSDN mới cho Live SDK rất sai, [LiveAuthClient (String) method] [1] nói tham số chuỗi là URI chuyển hướng. Nó không đề cập đến bất cứ điều gì về ClientID. [1] http://msdn.microsoft.com/en-us/library/live/microsoft.live.liveauthclient.aspx – Adarsha

1

Cảm ơn các mã ví dụ - đã giúp tôi đưa ra phiên bản cập nhật của mã cho Windows Phone 8, vv :) ​​


using System.Windows; 
using Microsoft.Live; 

public class LiveLogin : PhoneApplicationPage 
{ 
    private static readonly string[] _scopes = 
     new[] { 
     "wl.signin", 
     "wl.basic", 
     "wl.calendars", 
     "wl.calendars_update", 
     "wl.contacts_calendars", 
     "wl.events_create" }; 

    private LiveConnectClient _connection; 
    private LiveLoginResult _login; 

    public LiveLogin() 
    { 
     this.Loaded += this.OnLoaded; 
    } 

    private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs) 
    { 
     //---------------------------------------------------------------------- 
     // Login to skydrive 
     //---------------------------------------------------------------------- 
     await SkydriveLogin(); 
    } 

    private async Task SkydriveLogin() 
    { 
     try 
     { 
      //---------------------------------------------------------------------- 
      // Initialize our auth client with the client Id for our specific application 
      //---------------------------------------------------------------------- 
      LiveAuthClient authClient = new LiveAuthClient("**your client id here**"); 

      //---------------------------------------------------------------------- 
      // Using InitializeAsync we can check to see if we already have an connected session 
      //---------------------------------------------------------------------- 
      _login = await authClient.InitializeAsync(_scopes); 

      //---------------------------------------------------------------------- 
      // If not connected, bring up the login screen on the device 
      //---------------------------------------------------------------------- 
      if (_login.Status != LiveConnectSessionStatus.Connected) 
      { 
       _login = await authClient.LoginAsync(_scopes); 
      } 

      //---------------------------------------------------------------------- 
      // Initialize our connection client with our login result 
      //---------------------------------------------------------------------- 
      _connection = new LiveConnectClient(_login.Session); 
     } 
     catch (Exception ex) 
     { 
      //TODO: Add connection specific exception handling 
     } 
    } 
} 
+0

Mã này đưa ra lỗi sau - Ngoại lệ loại 'Microsoft.Live.LiveAuthException' xảy ra trong mscorlib.ni.dll nhưng không được xử lý trong mã người dùng –

10

Câu trả lời tuyệt vời Richard. Điều này thực sự đã giúp rất nhiều.

Tôi nhận thấy một vài nhận xét từ những người phàn nàn rằng họ không thể tìm thấy sự kiện InitializedCompleted. Nếu bạn đang mã hóa trong .Net 4.5 thì bạn cần phải làm theo mẫu async/await cho các phương thức không đồng bộ. Các lớp ở trên sẽ giống như thế này:

public class LiveLogin 
    { 
     private static readonly string[] Scopes = 
      new[] 
       { 
        "wl.signin", 
        "wl.basic", 
        "wl.calendars", 
        "wl.calendars_update", 
        "wl.contacts_calendars", 
        "wl.events_create" 
       }; 

     private LiveAuthClient _authClient; 



     public async Task<LiveConnectClient> Login() 
     { 
      _authClient = new LiveAuthClient("**your client id here**"); 

      LiveLoginResult result = await _authClient.InitializeAsync(Scopes); 
      if (result.Status == LiveConnectSessionStatus.Connected) 
      { 
       return new LiveConnectClient(result.Session); 
      } 
      result = await _authClient.LoginAsync(Scopes); 
      if (result.Status == LiveConnectSessionStatus.Connected) 
      { 
       return new LiveConnectClient(result.Session); 
      } 
      return null; 
     } 


    } 

MS có một async chờ mồi here

Các vấn đề liên quan