windows phone で active directory 認証 2011.12.1版

59
Part II アアアアアアアアア アアアアアアアアア アアアアアアアアアアアアア アアアアアアアア 安安 安 安安安安 一( 安安安安安 http://blogs.technet.com/junichi a/ アアアアアアアアア Windows Phone アアアアア ア アアアアアアアアアアアアアアアアアアアアア 2011.12.1 V1.01

Upload: junichi-anno

Post on 29-Nov-2014

2.705 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Windows Phone で Active Directory 認証 2011.12.1版

Part IIアプリケーションに認証機能を実装する日本マイクロソフト株式会社

エバンジェリスト

安納 順一(あんのう じゅんいち) http://blogs.technet.com/junichia/twitter @junichia

企業利用者のための Windows Phone アプリ開発~業務アプリケーションの配信と管理を実装する

2011.12.1 V1.01

Page 2: Windows Phone で Active Directory 認証 2011.12.1版

2

Agenda1. クラウドサービスとオンプレミス認証基盤の連携方法

• Windows Azure インスタンスと Active Directory の連携方法

2. Windows Phone から認証基盤にアクセスする方法• Silverlight アプリから Active Directory で認証する方法

3. WEB サービスが Active Directory で認証されたアプリケーションを受け入れる方法• WCF サービス が Active Directory の認証結果を受け入れる方法

お時間の都合上、この部分の詳しい解説は割愛させていただきます

Page 3: Windows Phone で Active Directory 認証 2011.12.1版

3

はじめに

Page 4: Windows Phone で Active Directory 認証 2011.12.1版

4

復習:業務サービスの構成

1. 業務サービスの場所① オンプレミス② 一部または全部がクラウド

2. 業務サービスへの接続アプリ① Internet Explorer② Silverlight アプリ

いずれの場合も認証基盤との連携が重要

認証基盤との連携方法が異なる

Page 5: Windows Phone で Active Directory 認証 2011.12.1版

5

業務アプリの「使い勝手」を考えれば ...

Exchange Server

Active Directory ファミリ

その他業務サーバー

IE9 Mobile

Silverlight アプリ

SharePoint Server

クラウドアプリ

Page 6: Windows Phone で Active Directory 認証 2011.12.1版

6

業務サービスの構成 ~ IE の場合※ブラウザコントロールも同様

業務サービス( WEB サイト)

認証基盤

業務サービス( WEB サイト)

ここが大問題!

オンプレミスクラウド

認証

認証

認証基盤に直接アクセスする必要は無い

Page 7: Windows Phone で Active Directory 認証 2011.12.1版

7

業務サービスの構成 ~ SL アプリの場合 ① 業務サービス

( SOAP/REST )

認証基盤

ここが問題!

オンプレミスクラウド

認証

業務サービス( SOAP/REST )

認証

認証基盤に直接アクセスする必要は無い

Page 8: Windows Phone で Active Directory 認証 2011.12.1版

8

業務サービスの構成 ~ SL アプリの場合 ② 業務サービス

( SOAP/REST )

認証基盤

オンプレミスクラウド

認可

業務サービス( SOAP/REST )

認証

認証基盤に直接アクセスする必要は無い

認可

ここが大問題!

認証基盤に直接アクセスする必要は無い

Page 9: Windows Phone で Active Directory 認証 2011.12.1版

9

課題はなに?

1. クラウドサービスとオンプレミス認証基盤の連携方法• Windows Azure インスタンスと Active Directory の連携方法

2. Windows Phone から認証基盤にアクセスする方法• Silverlight アプリから Active Directory で認証する方法

3. WEB サービスが Active Directory で認証されたアプリケーションを受け入れる方法• WCF サービス が Active Directory の認証結果を受け入れる方

法本日は時間の都合でここまでは解説できませんが、良質のサンプルが大量に存在します

Page 10: Windows Phone で Active Directory 認証 2011.12.1版

10

Windows Azure インスタンスとActive Directory の連携

Page 11: Windows Phone で Active Directory 認証 2011.12.1版

11

Windows Azure Connect

クラウドとオンプレミス間の仮想ネットワーク接続 Windows Azure インスタンスが

オンプレミスの一部になる

企業内システム

2011.11 現在 Open CTP

Page 12: Windows Phone で Active Directory 認証 2011.12.1版

12

Windows Azure Connect

どう使う?

Windows Azure アプリがオンプレミスの SQL Server にアクセス Windows Azure インスタンスを Active Directory ドメインに参加 Windows Azure アプリからファイル サーバーにアクセス Windows Azure アプリからオンプレミスのプリンターに印刷 Windows Azure アプリ /OS を PowerShell でリモート管理

Page 13: Windows Phone で Active Directory 認証 2011.12.1版

13

Windows Azure Connect の仕組み• 「ロール」と「エンドポイントグループ」間を Relay を通して接

続( IPv6 with IPSEC )

• 既存のネットワークも同時に使用可能Role

エンドポイント グループ

インスタンス インスタンス インスタンス

Firewall

Relay

IPSE

C

IPSEC

Azure Connectエンドポイントソフトウェア

Page 14: Windows Phone で Active Directory 認証 2011.12.1版

14

Windows Azure Connect を使用すると

業務サービス

認証基盤

業務サービス

オンプレミスクラウド

認証

認証

Windows Azure Connect により社内サーバーとの通信が可能に

Silverlight アプリIE mobile 9

認証基盤に直接アクセスする必要は無い

AD ドメインに参加

Page 15: Windows Phone で Active Directory 認証 2011.12.1版

15

Windows Azure Connect の設定方法1. ライセンス認証トークンを取得する

2. ライセンス認証トークンをプロジェクトに埋め込む

3. プロジェクトを Windows Azure にデプロイ

Page 16: Windows Phone で Active Directory 認証 2011.12.1版

16

4. オンプレミスのサーバーに Azure Connect エンドポイントソフトウェアをインストール( Azure インスタンスには自動的にインストールされる)

オンプレミスのサーバー上でこいつにアクセスすると

インストーラーがダウンロードされる

インストールが完了すると、 Azure Connect 専用の仮想ネットワークカードが作成され、 Azure Connect リレーサービスに接続される。

要再起動

Page 17: Windows Phone で Active Directory 認証 2011.12.1版

17

5. オンプレミスのサーバーと Azure インスタンスをグルーピング※グルーピングはプロジェクトのアップロードが完了してから

Azure Connect を有効にしたインスタンス

Azure Connect エンドポイントソフトウェアをインストールしたサーバー

構成が完了すると「!」マークが消える

Page 18: Windows Phone で Active Directory 認証 2011.12.1版

18

Active Directory ドメインに参加するにはプロジェクトに必要情報を埋め込んでからデプロイする• Microsoft.WindowsAzure.Plugins.Connect.EnableDomainJoin• Microsoft.WindowsAzure.Plugins.Connect.DomainFQDN• Microsoft.WindowsAzure.Plugins.Connect.DomainControll

erFQDN• Microsoft.WindowsAzure.Plugins.Connect.DomainAccountName• Microsoft.WindowsAzure.Plugins.Connect.DomainPasswor

d

encrypt する!

ここで指定したドメインコントローラにエンドポイントソフトウェアをインストールしておく

Page 19: Windows Phone で Active Directory 認証 2011.12.1版

19

パスワードの encrypt 方法サービス証明書を使用し、 PowerShell 等でパスワードを暗号化する

1. Visual Studio コマンドプロンプトを「管理者権限」で起動2. サービス証明書を作成

① 証明書( .cer )ファイルとプライベートキーファイル( .pvk )ファイルを作成

② pvk と .cer から .pfx ファイルを作成する

3. 作成した .pfx ファイルを当該 Azure インスタンスのサービス証明書としてアップロード

makecert -r -pe -n "CN=PwdEncCert” -sky exchange “PwdEncCert.cer” -sv “PwdEncCert.pvk"

pvk2pfx -pvk “PwdEncCert.pvk“ -spc “PwdEncCert.cer“ -pfx “PwdEncCert.pfx“ -pi < パスワード >

Page 20: Windows Phone で Active Directory 認証 2011.12.1版

20

5. 作成した .cer ファイルを開発環境にインストール※インストールした場所を覚えておくこと!(例えば CurrentUser\My )

6. PowerShell コンソールからの操作① インストールした証明書の Thumbprint を取得

PS C:\> cd cert:\CurrentUser\MyPS cert:\CurrentUser\my> dirThumbprint Subject---------- -------F8ACE------------------------FB827AC61A3 CN=127.0.0.1, O=TESTING ONLY, OU=W.EB805------------------------842E095355B CN=www.fabrikamshipping.com66EBD87D67398CD8FEA41E931B00BB98B8F06B06 CN=PwdEncCert・・

PS cert:\LocalMachine\my> cd c:\PS C:\>

Page 21: Windows Phone で Active Directory 認証 2011.12.1版

21

② Thumbprint を使用して暗号化PS C:\> $PlainPassword = “< パスワード >"PS C:\> $thumbprint = “ < Thumbprint > "PS C:\> $AzureCert = get-item cert:\LocalMachine\MY\$thumbprintPS C:\> [Reflection.Assembly]::LoadWithPartialName("System.Security")PS C:\> $pass = [Text.Encoding]::UTF8.GetBytes($PlainPassword)PS C:\> $content = new-object Security.Cryptography.Pkcs.ContentInfo –argumentList (,$pass)PS C:\> $env = new-object Security.Cryptography.Pkcs.EnvelopedCms $contentPS C:\> $env.Encrypt((new-object System.Security.Cryptography.Pkcs.CmsRecipient($AzureCert)))PS C:\> [Convert]::ToBase64String($env.Encode())

MIIBlAYJKoZIhvcNAQcDoIIBhTCCAYECAQAxggFFMIIBQQIBADApMBUxEzARBxxxxAMTClB3c0VuY0NlcnQCEF/PzJ/f6bOxxxxPlIhq7rUwDQYJKoZIhvcNAQEBBQAEggEAqkWA5vT7A8K8dvaxQOKWG5OegqeERbgzw2SOYwh9UqAyIMhEAm44YwV8DrYMykIMPJnwODV8f8M4M7FjRGJ9rrDjHJSf8W9DOJtsavEdZxG9gDEoZ3lfrjP/CvppnSh3Kxxxxxxxxxxxx3UONXGZNyBrA2hz+LcTbLs8BKFUDLlP++iearfPoQ26X0EcSnn6ImlkaRcJ7A0wh/TzZTtsaqA1I/2uF47hNlUSz0f55nGJloQUldSsf3L/NvtnjHmFNEiVLRpqgxxxxdzG9qA1LxWbxv9uDk09ObP6MFCQNg627v3FTHIVkUv2vcirOS1Su9QLpSInLq8352kUegoUjAzBgkqhkiG9w0BBwEwFAYIKoZIhvcNAwcECKgNvWGg707FgBDhojvrH17/nTKwlY23cPzs

PS C:\>

Page 22: Windows Phone で Active Directory 認証 2011.12.1版

22

7. 暗号化したパスワードを Microsoft.WindowsAzure.Plugins.Connect.DomainPassword にセット

8. 使用した証明書の Thumbprint をプロジェクトにセット

Page 23: Windows Phone で Active Directory 認証 2011.12.1版

23

Azure インスタンスがドメイン参加するまでの流れ

1. プロジェクトを展開2. オンプレミスにエンドポイントソフトウェアをインストール3. インスタンスとエンドポイントをグループ化

4. ドメイン参加完了

約15

Azure インスタンス側の Azure Connect エンドポイントソフトウェアがドメインコントローラと通信ができることを確認後、自動的にドメイン参加が行われる(再起動有)

インスタンスが複数ある場合には、全てのインスタンスがドメインに参加する

Page 24: Windows Phone で Active Directory 認証 2011.12.1版

24

IIS で Windows 認証を有効にすると

Widows Azure 上のインスタンス

Page 25: Windows Phone で Active Directory 認証 2011.12.1版

25

HTTP/HTTPS

Azure Connect

(参考) Windows Phone から社内ファイルサーバーにアクセス

SSTP (HTTPS )

企業内ネットワーク

Windows Azure Connect を使用してアクセス

HTTP/HTTPS

社内 AD による認証

WEBDAV

WCF サービス

IPv6 with IPSec

Page 26: Windows Phone で Active Directory 認証 2011.12.1版

26

SSL と Windows Phone

Windows Phone に規定でインストールされている Root Certificate詳細は http://msdn.microsoft.com/en-us/library/gg521150(v=VS.92).aspx

AOL (United States) RSA Security (United States)

Comodo (United States) SECOM Trust Systems Co. Ltd (Japan)

DigiCert (United States) Taiwan-CA Inc. (Taiwan)

Entrust (Canada) TrustCenter (Germany)

GlobalSign (United Kingdom) Trustwave (United States)

GoDaddy (United States) VeriSign (United States)

Keynectis (France) VeriSign Business (United States)

QuoVadis (Bermuda)

Page 27: Windows Phone で Active Directory 認証 2011.12.1版

27

自己署名証明書の取り扱いhttps:// ~ にアクセスしたときに証明書をインストールすることはできない

そこで

• 証明書を p7b 形式で保存• http アクセス可能な場所で公開

WCF を使用する場合には特に重要!!証明書がインストールされていないと「 There was no endpoint listening 」エラー

毎回表示されてうっとうしい

Page 28: Windows Phone で Active Directory 認証 2011.12.1版

28

Silverlight アプリからActive Directory で認証する方法

Page 29: Windows Phone で Active Directory 認証 2011.12.1版

29

Windows Phone と認証

Active Directory

Windows Live

Facebook

Google

SL.IdentityModel

Windows Live Connect SDK

Facebook C# SDK

SL.Phone.Federation Windows AzureAppFabric ACS

ライブラリ Identity Provider本日のテーマはこれ

Page 30: Windows Phone で Active Directory 認証 2011.12.1版

30

Windows Phone と Active Directory

• System.DirectoryServce がサポートされていない– Active Directory に直接アクセスできない– 何らかのサービスを介して認証の代行を依頼する必要がある

認証

利用 認証

AD DS

認証代行サービス

Page 31: Windows Phone で Active Directory 認証 2011.12.1版

31

Windows Phone と Active Directory Federation Service

① AD FS にはドメインのユーザー ID とパスワードを渡す(もちろん SSL )② AD 認証は AD FS が代行③ AD DS からユーザーの属性が返される(どんな属性を返すかは AD FS に定義されている)④ AD FS は「属性(クレーム)」に署名をしてセキュリティートークン( SAML アサーショ

ン)を生成⑤ WP アプリにはセキュリティトークンが返される

① UserID/Password ② 認証 AD DS

WS-Trust ( SOAP )

③ 属性⑤ セキュリティトークン

AD FS

④ 属性に署名

Page 32: Windows Phone で Active Directory 認証 2011.12.1版

32

セキュリティトークンの使い道

⑥ アプリケーションにセキュリティトークンを送付⑦ アプリケーションはセキュリティトークンを参照して「認可」

① UserID/Password ② 認証 AD DS

WS-Trust ( SOAP )

③ 属性⑤ セキュリティトークン

AD FS

④ 属性に署名

業務アプリ

⑥ セキュリティトークン

⑦ 認可

信頼

Page 33: Windows Phone で Active Directory 認証 2011.12.1版

33

クレーム とは アプリケーションに渡すユーザー自身の属性 ユーザーのロールを表現しアクセス認可に使用される

ユーザー ID

資格情報(認証結果)

メール アドレス

役職

氏名、姓、名

所属企業

性別

住所

趣味 /趣向

所属部門

言語

専門分野

会員番号

Page 34: Windows Phone で Active Directory 認証 2011.12.1版

34

セキュリティ トークン / アサーション パッケージ化されたクレーム 発行者(オーソリティ)の署名によって信頼性を担保 発行者とセキュリティトークン利用者は事前に信頼関係を結んでおく

セキュリティトークン / アサーション

クレーム 1

クレーム 2

クレーム n

署名

発行元による署名

Page 35: Windows Phone で Active Directory 認証 2011.12.1版

35

AD FS とは• Active Directory 5兄弟 の五男• AD 認証されたユーザーに対してセキュリティトーク

ンを発行する(セキュリティトークンサービス :STS )

Domain Service

Lightweight Directory Service

Certificate Service

Rights Management Service

Federation Service

社内ネットワーク DMZ

AD FS

load

bala

nce

AD DS

認証

STS

構成 DB

構成 DB

AD FS PROXY

load

bala

nce

R-PROXY

Internet

クレームストア

cluster

AD DS orSQL Server orAD LDS

orstandalone

orWID

Page 36: Windows Phone で Active Directory 認証 2011.12.1版

36

AD FS のセットアップ• 正確には Active Directory Federation Service 2.0• 最新版は WEB からダウンロード

http://www.microsoft.com/downloads/ja-jp/details.aspx?FamilyID=118C3588-9070-426A-B655-6CEC0A92C10B

• サポートしている OS• Windows Server 2008 Datacenter/• Windows Server 2008 Enterprise/• Windows Server 2008 R2/• Windows Server 2008 R2 Datacenter/• Windows Server 2008 R2 Enterprise/• Windows Server 2008 R2 Foundation/• Windows Server 2008 R2 Standard/• Windows Server 2008 Service Pack 2/• Windows Server 2008 Standard/• Windows Small Business Server 2008 Premium/• Windows Small Business Server 2008 Standard

• Active Directory ドメイン 必須

ADFS ダウンロード 検索

Page 37: Windows Phone で Active Directory 認証 2011.12.1版

37

Windows Phone から AD FS を利用するにはSL.IdentityModel ライブラリ• Identity Developer TrainingKit 2010 April に含まれている

http://www.microsoft.com/download/en/details.aspx?id=14347• PC 用 Silverlight 版なので移植が必要

\Claims├ Claim.cs├ ClaimCollection.cs├ ClaimsIdentity.cs├ ClaimsPrincipal.cs├ ClaimType.cs├ IClaimsIdentity.cs└ IClaimsPrincipal.cs

\Protocols\WSTrust├ IRequestCredentials.cs├ IssueCompletedEventArgs.cs├ IssuedTokenHeader.cs├ IWSTrustContract.cs├ RequestedSecurityToken.cs├ RequestSecurityToken.cs├ RequestSecurityTokenResponse.cs├ UsernameCredentials.cs├ WindowsCredentials.cs├ WSTrust13Constants.cs├ WSTrust13ResponseSerializer.cs├ WSTrustBinding.cs├ WSTrustBindingUsernameMixed.cs├ WSTrustBindingWindows.cs├ WSTrustClient.cs├ WSTrustRequestBodyWriter.cs└ WSTrustSerializationHelper.cs

\Services└ TokenCache.cs

Page 38: Windows Phone で Active Directory 認証 2011.12.1版

38

SL.IdentityModel ライブラリの移植手順1. プロジェクトを作成

• テンプレート: Windows Phone クラスライブラリ( Visual C# )

• 名前:(例) WP.IdentityModel とか

2. 参照設定の追加• System.Runtime.Serialization• System.ServiceModel• System.Xml.Linq

3. SL.IdentityModel から ソースをひたすらコピペ

Page 39: Windows Phone で Active Directory 認証 2011.12.1版

39

SL.IdentityModel を使用したアプリの作成1. 必要情報の収集

AD FS のエンドポイント https://<ADFS サーバー >/adfs/services/trust/13/usernamemixed

スコープ( ApplyTo 、業務アプリの識別名) (例) https://www.contoso.com/

2. 参照設定の追加• 移植したライブラリのプロジェクトを選択

3. using の追加• using SL.IdentityModel.Claims;• using SL.IdentityModel.Protocols.WSTrust;• using SL.IdentityModel.Services;

Page 40: Windows Phone で Active Directory 認証 2011.12.1版

40

WSTrustClient _client;

private void _btnLogin_Click(object sender, RoutedEventArgs e) { RequestST(); }

private void RequestST() {

_client = new WSTrustClient( new WSTrustBindingUsernameMixed(), new EndpointAddress("https://tfadfs.tf.com/adfs/services/trust/13/usernamemixed"), new UsernameCredentials(<ユーザー名 >, <パスワード >));

var rst = new RequestSecurityToken(WSTrust13Constants.KeyTypes.Bearer) {

AppliesTo = new EndpointAddress("https://www.contoso.com/") }; _client.IssueCompleted += client_IssueCompleted; _client.IssueAsync(rst); }

WS-Trust クライアントを作成

セキュリティトークンを非同期で要求

4. 認証&セキュリティトークンの取得

非同期で実行

Page 41: Windows Phone で Active Directory 認証 2011.12.1版

41

void client_IssueCompleted(object sender, IssueCompletedEventArgs e) { _client.IssueCompleted -= client_IssueCompleted;

if (e.Error == null) { Globals.RSTR = e.Result; MessageBox.Show( e.Result ); } else { MessageBox.Show(e.Error.Message); _btnLogin.IsEnabled = true; } }

Page 42: Windows Phone で Active Directory 認証 2011.12.1版

42

セキュリティトークン( SAML アサーション)<saml:Assertion MajorVersion="1" MinorVersion="1" AssertionID="_b6d37acb-381c-4160-a712-d3eceee44ef5" Issuer="http://tfadfs.tf.com/adfs/services/trust" IssueInstant="2011-11-28T00:16:18.445Z" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"><saml:Conditions NotBefore="2011-11-28T00:16:18.445Z" NotOnOrAfter="2011-11-28T01:16:18.445Z“><saml:AudienceRestrictionCondition><saml:Audience>https://www.contoso.com/</saml:Audience></saml:AudienceRestrictionCondition></saml:Conditions><saml:AttributeStatement><saml:Subject><saml:SubjectConfirmation><saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod></saml:SubjectConfirmation></saml:Subject><saml:Attribute AttributeName="name" AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims"><saml:AttributeValue>西城秀樹 </saml:AttributeValue></saml:Attribute><saml:Attribute AttributeName="emailaddress" AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims"><saml:AttributeValue>[email protected]</saml:AttributeValue></saml:Attribute><saml:Attribute AttributeName="companyname" AttributeNamespace="http://schemas.tf.com"><saml:AttributeValue> マイクロソフト </saml:AttributeValue></saml:Attribute><saml:Attribute AttributeName="department"AttributeNamespace="http://shcemas.tf.com"><saml:AttributeValue> エバンジェリズムグループ </saml:AttributeValue></saml:Attribute></saml:AttributeStatement><saml:AuthenticationStatement AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:password" AuthenticationInstant="2011-11-28T00:16:18.023Z"><saml:Subject><saml:SubjectConfirmation><saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod></saml:SubjectConfirmation></saml:Subject></saml:AuthenticationStatement>

Page 43: Windows Phone で Active Directory 認証 2011.12.1版

43

つづき<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><ds:Reference URI="#_b6d37acb-381c-4160-a712-d3eceee44ef5"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><ds:DigestValue>mT+IdNIJq6No0jKKX034keFNO+0HZ3dIbnOrDnzfnI4=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>fQ/ju362tt3EGKAMOtlfFwNAa7+dZoLftbNv1wyFjkytnPaD/BlxASXkDf2KeyBG8lxIpPDBTJFczME8A3gpHKBLuFIlURldx4V5ZuVkxzltLqjInHs/ectAzbagSHhDVYlRuo3F8zGoV2YFH4ZHme1snqPk0ArB60W5vh8jnAvmN7VsMovmmwPupMc5d4WLeXKH7/2Piz8Q2gM2sQb818lPhYzP9V80gJPPyDX6gbbn+QUjcnITv8nohZOutophufy54498Ul6EnBfFuEUToaZZCVfh6O5NaO/gXHp4XvNgWkRNbNM1IVYBHiOrQyTPrVrccLpYKMxed1j3yJS/Nw==</ds:SignatureValue><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><X509Data><X509Certificate>MIIC1jCCAb6gAwIBAgIQX4B1j9QgNZ9GI3IsMJ/+vTANBgkqhkiG9w0BAQsFADAnMSUwIwYDVQQDExxBREZTIFNpZ25pbmcgLSB0ZmFkZnMudGYuY29tMB4XDTExMDQxNTA1NDIwM1oXDTEyMDQxNDA1NDIwM1owJzElMCMGA1UEAxMcQURGUyBTaWduaW5nIC0gdGZhZGZzLnRmLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALcp//XyPz6m9F5ok4ebtuz7XsIdesuvlze5OgN2FD4PkAykETXHRgeheAq1TtbTN/nIiTy11kKgEAseyUSfvMGJarf49vui+zNcy37t+tbrBGdx+MRhuncTb7+O8FKJ3Wg/q9h8e10hq3VeEoxhzd4Ghdl1R4DfJYf10PztfFeQPvoD3FJNgca7v1BB1gRtUEEykmJvSoZFt18mhk1l+9Bsk5SJyEZaZc4M99ubqriVzzgDMZBa02+9Y6nmyy0P+sNrPnMSg031IulcGH+Zp8/W2B2ojHx3HisNkhYc8cQPYtLVoYanZ/qLG6hdqphM5TFi67QubdqclAE7MAoOx50CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAsU3E1uCieRjMAeVel29h9/oHLXy8hoGiy7iG3cgQBysA3Mlm3BjrLII4ze2JEZ74U8t5dk4K3K04emDuy4JhKpOP/iZXRN56UEuhIidFfsHi/k9PLXPEgG1AJnueK52u71xj8noyPJ1EsYwVWTqm5emN5CVDEpJtPFJmsdrBviChFaOO9XURrz2rcp+TAt56cDJKFqj39aKv5YAYWBiSj0UP5cSnXxI7F3lk7kNS19L2HdhPliXFoXI+doQ6ynOJrVUSqVMIbww7nOehWxI6TyWo9+A6TTl3LmpdNYyFYXJFjNI0JMKBm1U6yEvXEApI15LQ8IfJtSjPHoIPBHfSig==</X509Certificate></X509Data></KeyInfo></ds:Signature></saml:Assertion>

Page 44: Windows Phone で Active Directory 認証 2011.12.1版

44

次ページ

var sb = new StringBuilder(128);IClaimsPrincipal n = TokenHandler.Convert(Globals.RSTR);n.ClaimsIdentity.Claims.ToList().ForEach(vc => sb.AppendFormat("{0}\n {1}\n\n", vc.ClaimType, vc.Value));MessageBox.Show(sb.ToString()); public static class TokenHandler{ private static XNamespace ASSERTION_NAMESPACE = "urn:oasis:names:tc:SAML:1.0:assertion"; private const string CLAIM_VALUE_TYPE = "http://www.w3.org/2001/XMLSchema#string"; // bit of a hack

public static IClaimsPrincipal Convert(RequestSecurityTokenResponse rstr) { return new ClaimsPrincipal( GetClaimsIdentity( rstr ) ); }

private static ClaimsIdentity GetClaimsIdentity(RequestSecurityTokenResponse rstr) {

}

}

5. クレームの抽出

Page 45: Windows Phone で Active Directory 認証 2011.12.1版

45

private static ClaimsIdentity GetClaimsIdentity( RequestSecurityTokenResponse rstr ) { XDocument responseDoc = XDocument.Parse(rstr.RequestedSecurityToken.RawToken); XElement attStatement = responseDoc.Element(ASSERTION_NAMESPACE + "Assertion").Element(ASSERTION_NAMESPACE + "AttributeStatement");

var issuer = responseDoc.Root.Attribute("Issuer").Value; ClaimCollection claims = new ClaimCollection();

foreach (var c in attStatement.Elements(ASSERTION_NAMESPACE + "Attribute")) { string attrName = c.Attribute("AttributeName").Value; string attrNamespace = c.Attribute("AttributeNamespace").Value; string claimType = attrNamespace + "/" + attrName;

foreach (var val in c.Elements(ASSERTION_NAMESPACE + "AttributeValue")) { claims.Add(new Claim(issuer, issuer, claimType, val.Value, CLAIM_VALUE_TYPE)); } }

return new ClaimsIdentity(claims); }

Page 46: Windows Phone で Active Directory 認証 2011.12.1版

46

スコープについて• AD FS 上では「証明書利用者信頼」として定義されている• セキュリティトークンを渡す相手(業務アプリ)のこと• 定義されていないスコープにはセキュリティトークンは発行されない

① UserID/Password ② 認証 AD DSWS-Trust ( SOAP )

③ 属性⑤ セキュリティトークン

AD FS

④ 属性に署名

業務アプリ

⑥ セキュリティトークン

⑦ 認可

信頼

スコープ

Page 47: Windows Phone で Active Directory 認証 2011.12.1版

47

Page 48: Windows Phone で Active Directory 認証 2011.12.1版

48

クレームの定義について• クレームとは業務アプリケーションの「主張(要求)」• 業務アプリケーションに渡すクレームは AD FS で制御する• Active Directory や SQL Server に格納されている属性情報をクレーム

として送出可能• 属性情報は AD FS 内で結合や置換が可能(姓 + 名 = 氏名 等)

Page 49: Windows Phone で Active Directory 認証 2011.12.1版

49

サーバー証明書のインポート

private void _btnInstallCert_Click(object sender, RoutedEventArgs e) { CertP7BPath = “http://<WEB サーバー >/< 証明書のファイル名>.p7b"; WebBrowserTask webBrowserTask = new WebBrowserTask(); webBrowserTask.Uri = new Uri(CertP7BPath, UriKind.Absolute); webBrowserTask.Show(); }

SSL を要求するサービスの場合、証明書がインストールされていないとエンドポイントを見つけられない( There was no endpoint listening エラー)

Page 50: Windows Phone で Active Directory 認証 2011.12.1版

50

資格情報の再利用

ID/Password を送信

認証結果

ID/Password 入力

次回以降は起動したらすぐに認証開始

• 認証のために入力した資格情報( UserID/Password )は分離ストレージに保存して再利用することが可能

• 保存の際には暗号化することが望ましい

Page 51: Windows Phone で Active Directory 認証 2011.12.1版

51

DPAPI を使用した暗号化 / 復号化

using System.Text;using System.Security.Cryptography;

// 暗号化byte[] PinByte = Encoding.UTF8.GetBytes(<文字列>);byte[] ProtectedPinByte = ProtectedData.Protect(PinByte, null);

// 復号化byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);n = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);

いったん Byte型に変換してから暗号化する

Page 52: Windows Phone で Active Directory 認証 2011.12.1版

52

暗号化した文字列を参照するにはBase64 にエンコード

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();if (store.FileExists(encryptpin.FilePath_username) ) { MessageBox.Show( System.Convert.ToBase64String(encryptpin.ReadPinFromFile(encryptpin.FilePath_username)) ); }else { MessageBox.Show(" 資格情報は保存されていません "); }

Page 53: Windows Phone で Active Directory 認証 2011.12.1版

53

セキュリティトークンに対応したWCF サービスの作成方法

※WCF サービス が Active Directory の認証結果を受け入れる方法

本日は解説いたしません

Page 54: Windows Phone で Active Directory 認証 2011.12.1版

54

セキュリティトークンを受け取るメリット

• 情報の正当性が担保されている• STS による署名• STS との信頼関係

• クレームによりユーザーを認可(承認)できる• AD グループのメンバーシップ• 所属部署や役職 など

• アプリケーションの動作に必要な情報を受け取れる• 氏名• メールアドレス など

① UserID/Password ② 認証 AD DSWS-Trust ( SOAP )

③ 属性⑤ セキュリティトークン

AD FS

④ 属性に署名

業務アプリ

⑥ セキュリティトークン

⑦ 認可

信頼

アプリが固有に持つ必要が無い

取りに行く必要が無い

Page 55: Windows Phone で Active Directory 認証 2011.12.1版

55

Identity Developer Training Kit 2010 Aprilhttp://www.microsoft.com/download/en/details.aspx?id=14347

EXERCISE 1: USING WINDOWS IDENTITY FOUNDATION TO HANDLE AUTHENTICATION AND AUTHORIZATION IN A WCF SERVICE

EXERCISE 2: ACCEPTING TOKENS FROM AN ACTIVE DIRECTORY FEDERATION SERVICES (ADFS) STS

EXERCISE 3: INVOKING A WCF SERVICE ON THE BACKEND VIA DELEGATED ACCESS

Page 56: Windows Phone で Active Directory 認証 2011.12.1版

56

まとめ

Page 57: Windows Phone で Active Directory 認証 2011.12.1版

57

まとめ

• クラウドのミドルウェアは要注目です• Windows Azure Connect• Windows Azure AppFabric ACS• Windows Azure Sync Service など

• AD FS は AD DS の安全装置でもあります• アプリが直接 AD DS にアクセスするのを防げます

• この分野は「知ってる者勝ち」です

Page 58: Windows Phone で Active Directory 認証 2011.12.1版

58

リソース

安納の BLOGhttp://blogs.technet.com/junichia/

本セミナーのまとめサイトhttp://blogs.technet.com/b/junichia/p/wp7_and_ad.aspx

Page 59: Windows Phone で Active Directory 認証 2011.12.1版

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.

MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

© 2011 Microsoft Corporation.

All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.