Partager via


FormsCredentials, classe (Microsoft.Office.Server.Search.Federation)

Represents an object that contains the credentials used to connect to federated search locations that are configured to use forms authentication.

Espace de noms : Microsoft.Office.Server.Search.Federation
Assembly : Microsoft.Office.Server.Search (dans microsoft.office.server.search.dll)

Syntaxe

'Déclaration
Public Class FormsCredentials
    Inherits CookieCredentials
'Utilisation
Dim instance As FormsCredentials
public class FormsCredentials : CookieCredentials

Remarques

There are two classes that you can use to represent forms authentication data for federated search locations:

  • FormsAuthCredentials Use this class when you programmatically configure federated locations.

  • FormsCredentials Use this class in custom federated results Web Parts, when specifying credentials for federated locations that are configured for per user forms authentication.

Exemple

The following code example demonstrates how to set the user credentials on the Federated Results Web Part data source for a custom Federated Results Web Part. You must set user credentials in this way for federated locations that are configured to use any of the per-user authentication types, with the exception of Kerberos. For more information, see Création d'un composant WebPart personnalisé de recherche fédérée avec une interface utilisateur informations d'identification.

Notes

This code example is a simplified version of a custom Federated Results Web Part; it does not include the full code needed to prompt the user for their credentials. For a more complete example that demonstrates how to do this, see Exemple de composant WebPart pour la recherche fédérée personnalisé.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using Microsoft.Office.Server.Search.WebControls;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Federation;
using System.Security;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace CustomFedSearchSample
{
    [XmlRoot(Namespace = "CustomFederatedResultsWebPart")]
    public class SampleFederatedResultsWebPart : FederatedResultsWebPart, IPostBackEventHandler
    {
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                this.ShowMessages = false;
                base.OnLoad(e);
            }
            catch (Exception ex)
            {
                string x = ex.Message.ToString();
            }
        }

        protected override void ConfigureDataSourceProperties()
        {
            ICredentials userCredentials = null;
            base.ConfigureDataSourceProperties();
            FederatedResultsDatasource fedResultsDS = this.DataSource as FederatedResultsDatasource;
            LocationConfiguration locationConfig = null;

            if (fedResultsDS.Location != null)
            {
                string locationName = fedResultsDS.Location;
                LocationConfigurationCollection locations = SearchContext.Current.LocationConfigurations;
                locations.TryGetLocationConfigurationByInternalName(locationName, out locationConfig);

                switch (locationConfig.AuthInfo.AuthenticationType)
                {
                    case FederationAuthType.PerUserFormsAuthentication:
                        //Replace FormsLogonURL with the URL to the Forms logon page.
                        string logonURL = "http://FormsLogonURL";
                        //Replace FormMethod with post or get.
                        string logonMethod = FormMethod";
                        Dictionary<string, string> logonInput = new Dictionary<string, string>();
                        /*
                         *Code to compute logon input names and values for forms
                         *logon page, and add them to the Dictionary object goes here.
                         */ 
                        Dictionary<string, SecureString> logonSecureInput = new Dictionary<string, SecureString>();
                        /*
                         *Code to compute logon input names and System.Security.SecureString values
                         *for forms logon page and add them to the Dictionary object goes here.
                         */ 
                        CookieCollection formsLogonCookies = new CookieCollection();
                        userCredentials = new FormsCredentials(logonURL, logonURL, logonInput, logonSecureInput, logonMethod, formsLogonCookies);
                        break;

                    case FederationAuthType.PerUserCookie:
                        CookieCollection logonCookies = new CookieCollection();
                        /*
                         *Code to compute logon cookies and add them
                         *to the cookie collection goes here.
                         */
                        userCredentials = new CookieCredentials(logonCookies);
                        break;

                    case FederationAuthType.PerUserNTLM:
                    case FederationAuthType.PerUserBasicAuth:
                    case FederationAuthType.PerUserDigest:
                        /*
                         *Replace username< > with the Windows account.
                         *Replace domain with the Windows domain name.
                         *Replace password with the password for the Windows account.
                         */
                        string user = username";
                        string domain = "domain";
                        string password = "password";
                        userCredentials = new NetworkCredential(user, password, domain);
                        break;

                    default:
                        break;
                }
                if (fedResultsDS.UserCredentials.ContainsKey(locationName))
                {
                    fedResultsDS.UserCredentials.Remove(locationName);
                }

                if (userCredentials != null)
                {
                    fedResultsDS.UserCredentials.Add(locationName, userCredentials);
                }
            }
        }

        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            XPathNavigator federatedResults;
            federatedResults = base.GetXPathNavigator(viewPath);

            if (federatedResults == null)
            {
                ShowLogonControls();
            }
            else
            {
                ShowLogoutControls();
            }

            return federatedResults;
        }

        private void ShowLogonControls()
        {
            //Code to display logon controls goes here.
        }

        private void ShowLogoutControls()
        {
        //Code to display logout controls
        }
    }
}

Hiérarchie d'héritage

System.Object
   System.Net.NetworkCredential
     Microsoft.Office.Server.Search.Federation.CookieCredentials
      Microsoft.Office.Server.Search.Federation.FormsCredentials

Sécurité des threads

Les membres statiques publics de ce type (Shared en Visual Basic) sont sécurisés au niveau des threads. Il n'est pas garanti que les membres d'instance soient sécurisés au niveau des threads.

Voir aussi

Référence

Membres FormsCredentials
Microsoft.Office.Server.Search.Federation, espace de noms
UserCredentials

Autres ressources

Exemple de composant WebPart pour la recherche fédérée personnalisé
Vue d'ensemble de la recherche fédérée
Création d'un composant WebPart personnalisé de recherche fédérée avec une interface utilisateur informations d'identification