- 追加された行はこの色です。
- 削除された行はこの色です。
[[Open棟梁Project>http://opentouryo.osscons.jp/]] - [[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]
「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。
-[[戻る>ASP.NET Identity]]
* 目次 [#i42bb417]
#contents
*概要 [#x3f02a0a]
[[ASP.NET Identity]]は、以下の[[クレームベース認証]]のセキュアトークンサービス(STS)のEndpoint追加をサポートしている。
[[ASP.NET Identity]]のSTS実装のサポート状況
*プロトコル [#z3d3c75d]
*詳細 [#ufa4ac8d]
**[[ASP.NET IdentityのOAuth2によるSTS実装]] [#t880b797]
[[OAuth]] 2.0の[[基本4フロー>OAuth#c8a23ceb]]のセキュアトークンサービス(STS)機能をサポートしている。
**[[OAuth]] 2.0 [#w9606fd6]
-Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProviderクラスは、~
[[OAuth]]のセキュアトークンサービス(STS)のEndpoint追加をサポートしており、~
これにより、Java、JavaScript、Perl、PHP、Python、Rubyなどの「非 .NET 環境」と認証連携させることもできる。
*拡張 [#j102829c]
以下のように、「[[OAuth 2.0 拡張]]」や「[[OpenID Connect]]」拡張も可能。
-なお、[[OAuth]] 2.0は認可のプロトコルなので、認証の目的で使用する場合は[[セキュリティ>#c75e8910]]に注意が必要になる。
**[[ASP.NET IdentityのOAuth2拡張によるSTS実装]] [#w25bde9c]
**[[ASP.NET IdentityのOIDCによるSTS実装]] [#f4df076e]
**[[OpenID Connect]] [#cd0c97cc]
[[OpenID Connect]]のセキュアトークンサービス(STS)のEndpoint追加は、現時点(2016年)では提供されていない。
-https://github.com/jchannon/katanaproject/tree/master/src/Microsoft.Owin.Security.OpenIdConnect
-https://github.com/jchannon/katanaproject/tree/master/src/Microsoft.Owin.Security.OAuth
以下のようなライブラリもある模様(ただし、個人の成果物)
-https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server
-https://www.nuget.org/packages/AspNet.Security.OpenIdConnect.Extensions/
*準備 [#x35f5962]
OAuth2.0 Server、Client共に以下からダウンロード可能。
-[[The ASP.NET Site - OWIN OAuth 2.0 Authorization Server>#e91aecfc]] - [[Download the sample code. >#a54340f2]]
このサンプルは、VS2013・2015で、そのままF5実行可能で非常に便利。
* 検証 [#zd7b2804]
ダウンロードしたサンプルを使用して、以下のシナリオの検証ができる。
-[[Authorization Codeグラント種別>#q141d42e]]
-[[Implicitグラント種別>#qc6b7dec]]
-[[Resource Owner Password Credentialsグラント種別>#aace17a8]]
-[[Client Credentialsグラント種別>#bfaf5c00]]
**[[Authorization Codeグラント種別>OAuth#yfeb403d]] [#q141d42e]
**[[Implicitグラント種別>OAuth#m5c2d510]] [#qc6b7dec]
**[[Resource Owner Password Credentialsグラント種別>OAuth#zfff6f89]] [#aace17a8]
**[[Client Credentialsグラント種別>OAuth#o9473080]] [#bfaf5c00]
*その他 [#g4d5acce]
**Bearer Tokenを暗号化・復号化する秘密鍵 [#xcd8dac1]
-認可サーバ(AuthorizationServer)とリソースサーバ(ResourceServer)が同じマシン上に無い場合、~
Bearer Tokenを暗号化・復号化する秘密鍵を双方のマシン間で一致させる必要がある。
-これには、以下のように、machinekeyを両方のweb.configファイルに追加する必要がある。
<system.web>
<machineKey decryptionKey="Enter decryption Key here"
validation="SHA1"
validationKey="Enter validation Key here" />
</system.web>
***ツール [#ne3cf7a1]
machineKeyセクションの生成には、以下のツールが使えそう。
-インストール時に MachineKey を自動生成する NuGet パッケージを作った - しばやん雑記~
http://blog.shibayan.jp/entry/20150703/1435897073
--NuGet Gallery | MachineKeyGenerator 0.4.0~
https://www.nuget.org/packages/MachineKeyGenerator/
---garafu/MachineKeyGenerator:~
https://github.com/garafu/MachineKeyGenerator
>This tool allows you to generate random keys for validation and encryption/decription of the view state.
**ASP.NET IdentityでRFCに追加されている仕様 [#z90cfe4c]
[[RFC>OAuth#v698197b]]上の
-「MUST (しなければならない)」
-「MUST NOT (してはならない)」
-「REQUIRED (必須である)」
以外の部分で、強制されている実装を&color(red){朱書き};に設定した。
***[[Authorization Codeグラント種別>OAuth#y8bf613e]]について [#a0c59c8d]
-client_id
--引継:認可エンドポイントまで。
--検証:
---client_id + redirect_uriで、redirectエンドポイントの決定に使用される。
---&color(red){認可・Tokenエンドポイント間の一致は強制的+自動検証};
-redirect_uri
--引継:認可エンドポイントまで。
--検証:
---client_id + redirect_uriで、redirectエンドポイントの決定に使用される。
---client_idのredirectエンドポイントとの部分一致検証処理は自前で実装する。
---&color(red){認可・Tokenエンドポイント間の一致は強制的+自動検証};
-state
--引継:認可・Redirectエンドポイントまで自動引継
--検証:Redirectエンドポイントで自前で実装
-scope
--引継:
---認可エンドポイントまで。
---Tokenへの追加は自前で実装(Claimを使用)
--検証:Tokenからの取得・検証は自前で実装(Claimを使用)
***[[Implicitグラント種別>OAuth#h347a7c6]]について [#r8ad5c39]
-client_id~
--引継:認可エンドポイントまで。
--検証:client_id + redirect_uriで、redirectエンドポイントの決定に使用される。
-redirect_uri
--引継:認可エンドポイントまで。
--検証:
---client_id + redirect_uriで、redirectエンドポイントの決定に使用される。
---client_idのredirectエンドポイントとの部分一致検証処理は自前で実装する。
-state
--引継:認可・Redirectエンドポイントまで自動引継
--検証:自前で実装
-scope
--引継:
---&color(blue){アクセストークン・レスポンスに含める方法が不明};
---Tokenへの追加は自前で実装(Claimを使用)
--検証:Tokenからの取得・検証は自前で実装(Claimを使用)
***RFCに規定のない認証の仕掛け [#fa4583c6]
-認証には、(基本的に、)ASP.NET Identityを使用する。
-Bearer Tokenの発行には、ClaimsIdentityを使用する。
--ASP.NET MVCアプリケーションの認可エンドポイント上
---Cookie認証チケットからClaimsIdentityを生成できる。
AuthenticateResult ticket = this.AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie).Result;
ClaimsIdentity identity = (ticket != null) ? ticket.Identity : null;
---以下の方法でTokenを発行する。~
「Authorization Codeグラント種別」では仲介トークンを、~
「Implicitグラント種別」ではAccess Tokenを生成する。
this.AuthenticationManager.SignIn(identity);
--OAuthAuthorizationServerProviderのTokenエンドポイント上
---ユーザ情報を使用してClaimsIdentityを生成する。
ClaimsIdentity identity = await userManager.CreateIdentityAsync(
user, DefaultAuthenticationTypes.ExternalBearer);
---以下の方法でTokenを発行する。~
「Resource Owner Password Credentialsグラント種別」~
「Client Credentialsグラント種別」の両方でAccess Tokenを生成する。
context.Validated(identity);
-ClaimsIdentityにCustomの情報を格納する場合、以下の様にClaimを追加する。
identity.AddClaim(new Claim("urn:oauth:scope", scope));
***参考(サービスごとの仕様の違い) [#s99f3248]
以下が参考になる。
-OAuth2.0対応サービスのstate, redirect_uriパラメータの扱い色々 - BangBlog~
http://bang.hateblo.jp/entry/2012/10/09/002003
やはり、サービスによって扱いがマチマチなのは、~
redirect_uriパラメタとstateパラメタであるもよう。
*参考 [#k32a6edf]
-OAuthAuthorizationServerProvider クラス (Microsoft.Owin.Security.OAuth)~
https://msdn.microsoft.com/ja-jp/library/microsoft.owin.security.oauth.oauthauthorizationserverprovider.aspx
**The ASP.NET Site [#e91aecfc]
-OWIN OAuth 2.0 Authorization Server~
https://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server
このコンテンツは以下の様な構成になっている。
-[[Download the sample code.>#a54340f2]]
-[[Create an Authorization Server.>#ub84ef28]]
-[[Creating a Resource Server.>#ld778043]]
-[[Create OAuth 2.0 Clients.>#y247ad21]]
***Download the sample code. [#a54340f2]
以下から、サンプル・コードをダウンロードできる。
http://code.msdn.microsoft.com/OWIN-OAuth-20-Authorization-ba2b8783/file/114932/1/AuthorizationServer.zip
***Create an Authorization Server. [#ub84ef28]
-このAuthorization ServerはOAuthの4つのフローをサポートしている。
--Authorization Code Grant Client
--Implicit Grant Client
--Resource Owner Password Credentials Grant Client
--Client Credentials Grant Client
-サーバーの設定と実装
--OWIN Startup classでの設定
--OAuthAuthorizationServerProviderの実装
---ValidateClientRedirectUri~
(Authorization Code, Implicitグラント種別)
---ValidateClientAuthentication~
(Resource Owner Password Credentials, Client Credentialsグラント種別)
---GrantResourceOwnerCredentials~
(Resource Owner Password Credentialsグラント種別)
---GrantClientCredetails~
(Client Credentialsグラント種別)
--AuthorizeEndpointの実装~
(Authorization Code, Implicitグラント種別)
***Creating a Resource Server. [#ld778043]
アクセストークンによって保護されたResource ServerのEndpointを作成。
***Create OAuth 2.0 Clients. [#y247ad21]
.NETクライアント・アプリからHTTPアクセスする際のライブラリとしては、[[DotNetOpenAuth.OAuth2>https://www.nuget.org/packages/DotNetOpenAuth.OAuth2.Client]]を使用している。
-Authorization Code Grant Client~
WebApplicationとして実装。
-Implicit Grant Client~
JavaScriptで実装。
-Resource Owner Password Credentials Grant Client~
コンソール・アプリケーションで実装。
-Client Credentials Grant Client~
コンソール・アプリケーションで実装。
***関連する情報 [#tc543bde]
-The ASP.NET Forums
--OWIN and Authorization Code Grant Flow - Always Bad Request (Invalid Grant)~
https://forums.asp.net/t/1975505.aspx?OWIN+and+Authorization+Code+Grant+Flow+Always+Bad+Request+Invalid+Grant+
-Stack Overflow
--MVC 5 application - implement OAuth Authorization code flow~
http://stackoverflow.com/questions/25845420/mvc-5-application-implement-oauth-authorization-code-flow
--asp.net - Authorization_code grant flow on Owin.Security.OAuth: returns invalid_grant~
http://stackoverflow.com/questions/24515211/authorization-code-grant-flow-on-owin-security-oauth-returns-invalid-grant
-その他
--AuthServerTests.cs | Source Browser~
http://sourcebrowser.io/Browse/jchannon/katanaproject/tests/FunctionalTests/Facts/Security/AuthServer/AuthServerTests.cs#207
**その他 [#ad1e79f3]
実装方法の調査で参照にしたページ。
-ASP.NET WebAPI2でAjaxでOAuth認証するよ(ついでにTypeScriptとReact) - かずきのBlog@hatena~
http://blog.okazuki.jp/entry/2016/01/15/215901
-ASP.NET SPA (JavaScript) の Web API 認証 (ASP.NET Identity) – Tsmatz~
https://blogs.msdn.microsoft.com/tsmatsuz/2014/05/20/asp-net-spa-javascript-web-api-asp-net-identity-html5biz/
-asp.net - How do you consume extra parameters~
in OAuth2 Token request within .net WebApi2 application - Stack Overflow~
http://stackoverflow.com/questions/21243996/how-do-you-consume-extra-parameters-in-oauth2-token-request-within-net-webapi2
-c# - Can't get UserManager from OwinContext in apicontroller - Stack Overflow~
http://stackoverflow.com/questions/24001245/cant-get-usermanager-from-owincontext-in-apicontroller
-Simple OAuth Server: Implementing a Simple OAuth Server~
with Katana OAuth Authorization Server Components (Part 1) - Tugberk Ugurlu's Blog~
http://www.tugberkugurlu.com/archive/simple-oauth-server-implementing-a-simple-oauth-server-with-katana-oauth-authorization-server-components-part-1
----
Tags: [[:ASP.NET]], [[:ASP.NET MVC]], [[:ASP.NET SPA]], [[[[ASP.NET Web API]]]], [[:ASP.NET Identity]]
Tags: [[:.NET開発]], [[:ASP.NET]], [[:ASP.NET MVC]], [[:ASP.NET SPA]], [[ASP.NET Web API]], [[:ASP.NET Identity]], [[:認証基盤]], [[:セキュリティ]]
Special thanks go to mg-san for his support.