티스토리 뷰

Windows Azure – Mobile Services (3)

 

본 글에서는 Mobile Services를 통해 Windows 8 App에 인증을 손쉽게 구현해보도록 하겠습니다. 먼저 Windows Push Notifications & Live Connect 사이트에 로그온하여 내 앱 메뉴를 클릭합니다. 이미 작업했던 앱 이름을 확인할 수 있으며 앱 이름을 클릭하고 설정 변경에서  API 설정을 클릭합니다. 클라이언트 암호와 리디렉션 도메인을 아래 그림처럼 확인이 가능합니다. 클라이언트 암호는 Windows Azure Management Portal에서 사용할 것이며 리디렉션 도메인은 Windows Azure Management Portal Mobile Services 에서의 사이트 URL을 입력하면 됩니다.

(https://스이름.azure-mobile.net/)

 

Windows Azure Management Portal Mobile Services 로 이동하여 상세 정보 페이지로 이동하여 IDENTITY 메뉴에서 클리이언트 암호를 확인합니다.

DATA 메뉴를 클릭하고 TodoItem 을 클릭하여 권한을 인증된 사용자만 액세스 할 수 있도록 아래와 같이 설정합니다. 모든 권한을 Only Authenticated Users 로 선택하고 SAVE 메뉴를 클릭합니. 

 

Windows 8 App을 시작하고 저장이나 새로고침을 해보면 아래와 같은 401 오류가 발생하는 것을 알 수 있습니다.

 

Live SDK for Windows가 설치되어 있지 않다면 아래 URL을 통해 다운로드하여 설치합니다.

(http://msdn.microsoft.com/en-us/live/ff621310)

Windows 8 프로젝트의 솔루션 탐색기에서 참조 추가를 통해 Microsoft.Live.dll을 추가합니다.

C:\Program Files (x86)\Microsoft SDKs
\Live\v5.0\Metro XAML\References\CommonConfiguration\Neutral

 

개체 탐색기를 통해 Microsoft.Live.dll을 확인할 수 있습니다.

 

MainPage.xaml.cs 에 아래와 같은 using 구문을 추가합니다.

using Microsoft.Live;

using Windows.UI.Popups;

 

session 이라는 필드와 Authenticate 메서드를 MainPage 클래스에 아래와 같이 선언합니다.

private LiveConnectSession session;

 

 

private async System.Threading.Tasks.Task Authenticate()

{

LiveAuthClient liveIdClient = new LiveAuthClient("<<TODO Redirect URL here>>");

 

while (session == null)

{

       if (liveIdClient.CanLogout)

liveIdClient.Logout();

 

 

      LiveLoginResult result = await liveIdClient.LoginAsync(new[] { "wl.basic" });

      if (result.Status == LiveConnectSessionStatus.Connected)

      {

          session = result.Session;

          LiveConnectClient client = new LiveConnectClient(result.Session);

          LiveOperationResult meResult = await client.GetAsync("me");

          MobileServiceUser loginResult = await App.MobileService.LoginAsync(session.AuthenticationToken);

 

string title = string.Format("환영 {0} !", meResult.Result["first_name"]);

          var message = string.Format("로그인 ID - {0}", loginResult.UserId);

          var dialog = new MessageDialog(message, title);

          ialog.Commands.Add(new UICommand("OK"));

          await dialog.ShowAsync();

      }

      else

      {

          session = null;

          var dialog = new MessageDialog("로그인 필요");

          dialog.Commands.Add(new UICommand("OK"));

          await dialog.ShowAsync();

 

      }

 }

}

}

 

위 코드에서 <<TODO Redirect URL here>> Mobile Servicessite url을 입력합니다.

그리고 OnNavigtedTo 이벤트를 아래와 같이 수정합니다.

protected async override void OnNavigatedTo(NavigationEventArgs e)

        {

            await Authenticate();

            RefreshTodoItems();

        }

 

F5를 통해 App을 시작하면 아래와 같은 인증 창이 나타나는 것을 확인 가능합니다. 손쉽게 Live 인증에 대한 내용을 추가할 수 있다는 것을 알 수 있습니다.

로그인에 성공하면 코드에서처럼 dialog가 나타나게 됩니다.  

이상으로  Windows Azure에서 제공되는 Mobile Services 에 대한 Data, Push Notification, Authentication에 대한 내용을 알아보았습니다. 이를 통해 Windows 8 App에 대한 기능을 더 손쉽게 추가할 수있다는 것을 확인할 수 있습니다.

 

'Microsft Azure' 카테고리의 다른 글

Windows Azure – Mobile Services (4)  (0) 2012.10.05
Windows Azure Update - 9월  (0) 2012.09.20
Windows Azure – Mobile Services (2)  (0) 2012.09.07
Windows Azure - Mobile Services (1)  (0) 2012.09.03
Windows Azure - Mobile Services  (0) 2012.08.29
댓글