Procedimiento para obtener todos los proveedores de notificaciones asociados con una aplicación web en SharePoint 2010
Artículo original publicado el domingo, 27 de marzo de 2011
Me han hecho esta pregunta un par de veces antes, por lo que me gustaría poder averiguar mediante programación qué proveedores de notificaciones se usan con una aplicación web. Por lo general, esta pregunta se hace para preguntar qué SPTrustedIdentityTokenIssuers se usan, pero el método que demostraré revelará esos proveedores, así como los proveedores de notificaciones personalizados que no están habilitados de manera predeterminada (porque si están habilitados de manera predeterminada, se pueden usar en todas partes).
Lo primero que hay que entender es que si pregunta sobre lo que está habilitado para una aplicación web, seguramente tiene una idea equivocada acerca de esto (y probablemente esa es la razón por la que los usuarios tienen dificultades para encontrar esta información). Los proveedores de notificaciones se van a aplicar en el nivel de la zona, no en el nivel de la aplicación web. Dada una dirección URL de SharePoint, ¿cómo averiguamos esta información?
Para empezar, obtenga un nuevo SPSite según la dirección URL en la que está interesado:
using (SPSite theSite = new SPSite("https://someUrl"))
{
…
}
Una vez que tenga el objeto SPSite puede obtener la aplicación web y la zona:
//get the web app
SPWebApplication wa = theSite.WebApplication;
//get the zone for the site
SPUrlZone theZone = theSite.Zone;
Con esa información, puede obtener el SPIisSettings para la zona, que es donde reside la mayor parte de las ventajas:
//get the settings associated with the zone
SPIisSettings theSettings = wa.GetIisSettingsWithFallback(theZone);
Una vez que tengo la información de la zona, puedo obtener los proveedores de notificaciones y los proveedores de autenticación para esa zona. Se encuentran en estas dos propiedades: ClaimsAuthenticationProviders y ClaimsProviders. Tenga en cuenta que cada propiedad ClaimsAuthenticationProvider tiene solo un subconjunto muy pequeño de la información que se obtiene al hacer algo como Get-SPTrustedIdentityTokenIssuers en PowerShell. Si realmente desea obtener el objeto subyacente principal, debe tomar la propiedad ClaimsAuthenticationProvider y obtener un SPTrustedLoginProvider de esta. Afortunadamente esto tampoco es muy complicado. Este es un ejemplo donde básicamente estoy consultando una lista de SPTrustedLoginProviders mediante LINQ. Observe que en el ejemplo solo me interesan los proveedores de notificaciones de SAML (también conocidos como 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();
}
}
}
Para completar, voy a pegar todo el bloque de código aquí abajo. En este escenario particular, estaba buscando todos los SPTrustedIdentityTokenIssuers asociados a una zona y, para cada uno, creé una cadena con el nombre del proveedor y la dirección URL a la que se va a redireccionar para autenticar durante su uso con ese proveedor.
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);
}
}
}
}
Esta entrada de blog es una traducción. Puede consultar el artículo original en How To Get All Claims Providers Associated with a Web Application in SharePoint 2010