SharePoint 2010 で Web アプリケーションに関連付けられているすべてのクレーム プロバイダーを取得する方法

 原文の記事の投稿日: 2011 年 3 月 27 日 (日曜日)

これまでに何度かこの質問を受けたので、自分の Web アプリケーションでどのようなクレーム プロバイダーが使用されているかをプログラミングで検出できるようにしたいと思いました。 通常、この質問にはどのような SPTrustedIdentityTokenIssuer が使用されているかという意味がありますが、ここで紹介する方法では、これだけでなく、既定では有効ではないカスタム クレーム プロバイダーも検出します (既定で有効だったら、どこででも使用されてしまうからです) 。

理解すべき最初の点は、Web アプリケーションで何が有効かと考える場合に、思い違いをすることです (そしておそらく、これがこの情報をなかなか見つけられない理由です)。 クレーム プロバイダーは、Web アプリケーション レベルではなく、ゾーン レベルで適用されます。 それでは、SharePoint の URL が提供された場合は、どのようにこの情報を見つけられるでしょうか。

始めに、対象となる URL に基づいて新しい SPSite を取得します。

using (SPSite theSite = new SPSite("https://someUrl"))

{

}

SPSite オブジェクトを入手したら、Web アプリケーションとゾーンを取得できます。

//get the web app

SPWebApplication wa = theSite.WebApplication;

//get the zone for the site

SPUrlZone theZone = theSite.Zone;

 

この情報があれば、ゾーンの SPIisSettings を取得できます。ここに、役立つ情報の大半があります。

//get the settings associated with the zone

SPIisSettings theSettings = wa.GetIisSettingsWithFallback(theZone);

 

ゾーンの情報を入手したら、そのゾーンに対する認証プロバイダーとクレーム プロバイダーの両方を取得できます。 これらは、2 つのプロパティー ClaimsAuthenticationProviders と ClaimsProviders にあります。 各 ClaimsAuthenticationProvider には、PowerShell で Get-SPTrustedIdentityTokenIssuers のようなことを実行した場合に取得する情報のほんの一部しか含まれていないことに注意してください。 中核を成す基本的なオブジェクトを実際に取得するには、ClaimsAuthenticationProvider を入手し、そこから SPTrustedLoginProvider を取得する必要があります。 幸いにも、どちらもそれほど大変ではありません。 以下に例を示します。基本的には LINQ を使用して SPTrustedLoginProvider のリストを照会しています。この例では、SAML クレーム プロバイダー (すなわち、SPTrustedIdentityTokenIssuer) のみを対象としていることに注意してください。

//get the token service manager so we can retrieve the appropriate

//trusted login provider

SPSecurityTokenServiceManager sptMgr = SPSecurityTokenServiceManager.Local;

                                 

//get the list of authentication providers associated with the zone

foreach (SPAuthenticationProvider prov in theSettings.ClaimsAuthenticationProviders)

{

//make sure the provider we're looking at is a SAML claims provider

       if (prov.GetType() == typeof(Microsoft.SharePoint.Administration.SPTrustedAuthenticationProvider))

       {

       //get the SPTrustedLoginProvider using the DisplayName

              var lp =

                     from SPTrustedLoginProvider spt in

                     sptMgr.TrustedLoginProviders

                     where spt.DisplayName == prov.DisplayName

                     select spt;

              //there should only be one match, so retrieve that

              if ((lp != null) && (lp.Count() > 0))

              {

                     //get the login provider

                     SPTrustedLoginProvider loginProv = lp.First();

              }

       }

}

 

万全を期すため、以下にコード ブロック全体を貼り付けます。 この特定のシナリオでは、ゾーンに関連付けられているすべての SPTrustedIdentityTokenIssuer を検索し、それぞれに対し、プロバイダーの名前と、そのプロバイダーの使用時に認証のためにリダイレクトされる URL で構成される文字列を作成ました。

using (SPSite theSite = new SPSite("https://someUrl"))

{

//get the web app

       SPWebApplication wa = theSite.WebApplication;

       //get the zone for the site

       SPUrlZone theZone = theSite.Zone;

       //get the settings associated with the zone

       SPIisSettings theSettings = wa.GetIisSettingsWithFallback(theZone);

       //if this isn't a claims auth site then bail out

       if (!theSettings.UseTrustedClaimsAuthenticationProvider)

       {

              MessageBox.Show("This is not a SAML claims auth site");

              return;

       }

                                 

       //clear the list of providers out

       ProviderLst.Items.Clear();

       //get the token service manager so we can retrieve the appropriate

       //trusted login provider

       SPSecurityTokenServiceManager sptMgr = SPSecurityTokenServiceManager.Local;

                                 

       //get the list of authentication providers associated with the zone

       foreach (SPAuthenticationProvider prov in

              theSettings.ClaimsAuthenticationProviders)

       {

       //make sure the provider we're looking at is a SAML claims provider

              if (prov.GetType() ==

       typeof(Microsoft.SharePoint.Administration.SPTrustedAuthenticationProvider))

              {

                     //get the SPTrustedLoginProvider using the DisplayName

                     var lp =

                           from SPTrustedLoginProvider spt in

                           sptMgr.TrustedLoginProviders

                           where spt.DisplayName == prov.DisplayName

                           select spt;

                     //there should only be one match, so retrieve that

                     if ((lp != null) && (lp.Count() > 0))

                     {

                           //get the login provider

                           SPTrustedLoginProvider loginProv = lp.First();

                           //get the login info

                           string provInfo = prov.DisplayName + " - " +

                                  loginProv.ProviderUri.ToString();

                           //add the login info to the list

                           ProviderLst.Items.Add(provInfo);

                     }

              }

       }

}

 

これはローカライズされたブロク投稿です。原文の記事は、「How To Get All Claims Providers Associated with a Web Application in SharePoint 2010」をご覧ください。