windows.web.http.httpclientとwebauthenticationbroker

20
Windows.Web.Http.HttpClie nt と WebAuthenticationBroker と Web とととととととと 9 とととととととととと 2014/7/26 とと とと (@kumar0001)

Upload: nobuaki-aoki

Post on 10-May-2015

479 views

Category:

Software


0 download

DESCRIPTION

第9回まどべんよっかいちでの発表資料です。最後のWP8やWPFに関するページは後日に追記予定です。

TRANSCRIPT

Page 1: Windows.Web.Http.HttpClientとWebAuthenticationBroker

Windows.Web.Http.HttpClient とWebAuthenticationBroker で Web

サービスアクセス

@第 9 回まどべんよっかいち2014/7/26青木 宣明 (@kumar0001)

Page 2: Windows.Web.Http.HttpClientとWebAuthenticationBroker

今日の目標

Windows ストアアプリにおいてWindows.Web.Http.HttpClient とWebAuthenticationBroker を使ってOAuth 認可& REST アクセスをする

Windows Phone 8.1 で WAB を使うときの注意点を知ってもらう

WPF で使える System.Net.Http 名前空間の HttpClient を知ってもらう

Page 3: Windows.Web.Http.HttpClientとWebAuthenticationBroker

Web サービスへのアクセス

OAuth でリソースへのアクセス権限を得る

• リクエストトークンの取得• ユーザ承認画面にアクセスしてリソースアクセス許可を得る• アクセストークンを取得

リソースアクセスの URI に HTTP アクセスする

• 認証情報の設定• 非同期での HTTP アクセス

処理の流れ

HttpClient

HttpClientWebAuthenticationBroker

Page 4: Windows.Web.Http.HttpClientとWebAuthenticationBroker

OAuth とは

Oauth って何?• OAuth is an open standard for

authorization. OAuth provides client applications a 'secure delegated access' to server resources on behalf of a resource owner.http://en.wikipedia.org/wiki/OAuth

つまり• サーバ上のリソースへのアクセス権を認

可のためのプロトコル

Page 5: Windows.Web.Http.HttpClientとWebAuthenticationBroker

OAuth による Web サービスアクセスTwitter (OAuth 1.0a)

③④

Page 6: Windows.Web.Http.HttpClientとWebAuthenticationBroker

OAuth による Web サービスアクセスIIJmio (OAuth 2.0)

(Implicit Grant)

Page 7: Windows.Web.Http.HttpClientとWebAuthenticationBroker

ここらでデモでも

Page 8: Windows.Web.Http.HttpClientとWebAuthenticationBroker

OAuth の処理

HTTP GET/POST で OAuth のエンドポイントにアクセスする

• ヘッダなどへの認証情報の設定• 非同期アクセス

承認画面の URI にアクセスして、その結果のリダイレクト URI を受け取る

• ブラウザでのアクセス• リダイレクト URI の解析

共通した処理

HttpClient

WebAuthenticationBroker

Page 9: Windows.Web.Http.HttpClientとWebAuthenticationBroker

Windows.Web.Http.HttpClient

REST での Web サービスアクセスに便利• GET/POST/PUT/DELETE に対応した非同期メ

ソッドGetAsync/PostAsync/PutAsync/DeleteAsync

• 連鎖可能なフィルタ (IHttpFilter)認証情報の付加など、 HTTP 通信を補助的に加工する処

理を追加できる

http://msdn.microsoft.com/en-us/library/windows/apps/bg182884.aspx

Page 10: Windows.Web.Http.HttpClientとWebAuthenticationBroker

HttpClient のコンストラクタフィルタを指定

フィルタを指定しない場合はデフォルトのHttpBaseProtocolFilter が使用される

IHttpFiler を実装するクラス

Page 11: Windows.Web.Http.HttpClientとWebAuthenticationBroker

IHttpFilter2 個のメソッドを実装・ SendRequestAsync: HTTP リクエストを送信して結果を返す・ Dispose: リソースの廃棄

ヘッダに認証情報を追加している

Page 12: Windows.Web.Http.HttpClientとWebAuthenticationBroker

HTTP アクセスGET の場合

GET アクセスは GetStringAsync 以外に 3 パターン・ GetAsync / GetBufferAsync / GetInputStreamAsync  ( 結果の返し方が異なる )

結果を文字列で取得する版

戻り値は IAsyncOperationWithProgress<String, HttpProgress>

Page 13: Windows.Web.Http.HttpClientとWebAuthenticationBroker

HTTP アクセスPOST の場合

IHttpContent を実装したクラスを PostAsync に指定する

戻り値はIAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>

Page 14: Windows.Web.Http.HttpClientとWebAuthenticationBroker

HTTP アクセスPUT の場合

IHttpContent を実装したクラスを PutAsync に指定する

戻り値はIAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>

Page 15: Windows.Web.Http.HttpClientとWebAuthenticationBroker

IHttpContent

クラス 説明HttpBufferContent バッファ (IBuffer) を使用するコンテンツ

( バイナリデータなど )HttpFormUrlEncodedContent MIME タイプ ” application/x-www-form-urlencoded”

名前 / 値のペアから構成されるコンテンツHttpStreamContent ストリーム (IInputStream) を使用するコンテンツHttpStringContent 文字列を使用するコンテンツ

(JSON,XML データなど )

HttpMultipartContent MIME タイプ “ multipart/*” のコンテンツHttpMultipartFormDataContent MIME タイプ “ multipart/form-data” のコンテンツ

( form タグで送信されるフォームデータ )

Request/Response

Request

Page 16: Windows.Web.Http.HttpClientとWebAuthenticationBroker

ユーザ承認画面への遷移

WPF など従来は…1. WebBrowser コントロールをログイン画面にホストする2. 認証画面の URL を指定して表示する3. ユーザの承認操作の後、 Navigated イベントを監視して

コールバック URL にリダイレクトされたのを検知する4. リダイレクトされた URL のパラメータからトークンなど

を取得する

WebBrowserOAuth 処理② 認証画面の表示

③Navigated イベント④URL からトークン取得

①WebBrowser コントロールをホスト

Page 17: Windows.Web.Http.HttpClientとWebAuthenticationBroker

WebAuthenticationBroker1. 認証画面の URL を指定して非同期で実行。画面はブローカー

が表示する2. 認証終了後に成功・失敗の結果を見る

• 成功ならリダイレクト先 URL のパラメータからトークンなどを取得

Page 18: Windows.Web.Http.HttpClientとWebAuthenticationBroker

2つの HttpClient

WPF など Windows 8.1 より前ではSystem.Net.Http.HttpClient を使う• それ以外のクラスも同様の機能を持つク

ラスがあるHttpClient ⇒ HttpClientIHttpFiler ⇒ HttpMessageHandlerHttpContent ⇒ HttpContent…

• 詳細はデモコードで

Page 19: Windows.Web.Http.HttpClientとWebAuthenticationBroker

WP8.1 での注意点

WebAuthenticationBroker はアプリを中断させる• 認証が終わるとアプリが起動されて

Activated イベントが発生するイベント引数から認証終了後の起動か判別し

て残りの処理を行う• 詳細はデモコードで

Page 20: Windows.Web.Http.HttpClientとWebAuthenticationBroker

まとめ

HttpClient で非同期・認証情報の処理が手軽に

• IHttpFilter で認証情報の設定を HTTP 通信処理から分離できる• async/await で非同期処理を扱える• GET/POST/PUT/DELETE に対応したアクセスメソッド

WebAuthenticationBroker で認証画面の処理を手軽に

• 認証画面 URL とコールバック URL を指定すれば、ユーザ承認後の処理が自動化される

• ユーザ承認に成功したらリダイレクト URL のパラメータを処理する