Freigeben über


Codeausschnitt: Abrufen von Benutzeranmeldeinformationen mithilfe des standardmäßigen Anbieters für einmaliges Anmelden

Letzte Änderung: Freitag, 14. Mai 2010

Gilt für: SharePoint Server 2010

Im folgenden Codebeispiel wird veranschaulicht, wie mithilfe des standardmäßigen Anbieters für einmaliges Anmelden Benutzeranmeldeinformationen abgerufen werden.

Voraussetzungen:

  • Microsoft SharePoint Server 2010

  • Microsoft .NET Framework 3.5

So verwenden Sie dieses Beispiel

  1. Starten Sie Microsoft Visual Studio, und erstellen Sie ein neues C#-Konsolenanwendungsprojekt. Wählen Sie beim Erstellen des Projekts .NET Framework 3.5 aus.

  2. Wählen Sie im Menü Ansicht den Befehl Eigenschaftenseiten aus, um die Projekteigenschaften aufzurufen.

  3. Wählen Sie auf der Registerkarte Erstellen unter Zielplattform die Option Any CPU aus.

  4. Schließen Sie das Fenster mit den Projekteigenschaften.

  5. Entfernen Sie im Projektmappen-Explorer unter Verweise sämtliche Projektverweise bis auf System und System.Core.

  6. Fügen Sie dem Projekt die folgenden Verweise hinzu:

    1. Microsoft.BusinessData

    2. Microsoft.Office.SecureStoreService

    3. Microsoft.SharePoint

    4. System.Web

  7. Ersetzen Sie den automatisch generierten Code in Program.cs durch den Code am Ende dieser Prozedur.

  8. Ersetzen Sie den Wert von appId durch den Namen Ihrer Zielanwendung für einmaliges Anmelden. Beachten Sie, dass es sich bei der in diesem Beispiel verwendeten Zielanwendung für einmaliges Anmelden um eine Anwendung vom Typ Individual handelt, die Folgendes enthält: Benutzername (nicht der Windows-Benutzername), Kennwort (nicht das Windows-Kennwort) und eine PIN.

  9. Drücken Sie F6, um die Lösung zu erstellen.

  10. Drücken Sie STRG+F5, um das Beispiel auszuführen.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.BusinessData.Infrastructure.SecureStore;
using Microsoft.Office.SecureStoreService.Server;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Microsoft.SDK.Sharepoint.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the default Secure Store Service provider.
            ISecureStoreProvider provider = SecureStoreProviderFactory.Create();
            if (provider == null)
            {
                throw new InvalidOperationException("Unable to get an ISecureStoreProvider");
            }

            ISecureStoreServiceContext providerContext = provider as ISecureStoreServiceContext;
            providerContext.Context = SPServiceContext.GetContext(GetCentralAdminSite()); 

            // Create the variables to hold the credentials.
            string userName = null;
            string password = null;
            string pin = null;
            // Specify a valid target application ID for the Secure Store.
            string appId = "mySecureStoreTargetApplication";

            try
            {
                // Because we are getting the credentials in the using block, all the credentials that we get 
                // will be disposed after the using block. If you need to cache the credentials, do not 
                // use the using block, and dispose the credentials when you are finished.
                //
                // In the following block, we are looking for the first user name, password, and pin
                // credentials in the collection.
                using (SecureStoreCredentialCollection creds = provider.GetCredentials(appId))
                {
                    // Secure Store Service will not return null. It may throw a SecureStoreServiceException,
                    // but this may not be true for other providers.
                    Debug.Assert(creds != null);

                    if (creds != null)
                    {
                        foreach (SecureStoreCredential cred in creds)
                        {
                            if (cred == null)
                            {
                                // Secure Store Service will not return null credentials, but this may not be true for other providers.
                                continue;                    
                            }

                            switch (cred.CredentialType)
                            {
                                case SecureStoreCredentialType.UserName:
                                    if (userName == null)
                                    {
                                        userName = GetStringFromSecureString(cred.Credential);
                                    }
                                    break;

                                case SecureStoreCredentialType.Password:
                                    if (password == null)
                                    {
                                        password = GetStringFromSecureString(cred.Credential);
                                    }
                                    break;

                                case SecureStoreCredentialType.Pin:
                                    if (pin == null)
                                    {
                                        pin = GetStringFromSecureString(cred.Credential);
                                    }
                                    break;
                            }
                        }
                    }
                }

                if (userName == null || password == null || pin == null)
                {
                    throw new InvalidOperationException("Unable to get the credentials");
                }

                // Use the credentials.
                //
                // Note that it is not a secure programming practice to print credential information, but this code example 
                // prints the credentials to the console for testing purposes.
                Console.WriteLine("User Name: " + userName);
                Console.WriteLine("Password : " + password);
                Console.WriteLine("Pin      : " + pin);
            }
            catch (SecureStoreException e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        private static string GetStringFromSecureString(SecureString secStr)
        {
            if (secStr == null)
            {
                return null;
            }

            IntPtr pPlainText = IntPtr.Zero;
            try
            {
                pPlainText = Marshal.SecureStringToBSTR(secStr);
                return Marshal.PtrToStringBSTR(pPlainText);
            }
            finally
            {
                if (pPlainText != IntPtr.Zero)
                {
                    Marshal.FreeBSTR(pPlainText);
                }
            }
        }

        public static SPSite GetCentralAdminSite()
        {
            SPAdministrationWebApplication adminWebApp = SPAdministrationWebApplication.Local;
            if (adminWebApp == null)
            {
                throw new InvalidProgramException("Unable to get the admin web app");
            }

            SPSite adminSite = null;
            Uri adminSiteUri = adminWebApp.GetResponseUri(SPUrlZone.Default);
            if (adminSiteUri != null)
            {
                adminSite = adminWebApp.Sites[adminSiteUri.AbsoluteUri];
            }
            else
            {
                throw new InvalidProgramException("Unable to get Central Admin Site.");
            }

            return adminSite;
        }
   }
} 

Siehe auch

Referenz

SecureStoreProviderFactory

Create()

ISecureStoreProvider

ISecureStoreServiceContext

SPServiceContext

GetCredentials(String)

SecureStoreCredentialCollection

SecureStoreCredential

CredentialType

SPAdministrationWebApplication