Partager via


EndpointIdentity.CreateUpnIdentity(String) Méthode

Définition

Crée une identité UPN (user principal name) portant le nom spécifié.

public:
 static System::ServiceModel::EndpointIdentity ^ CreateUpnIdentity(System::String ^ upnName);
public static System.ServiceModel.EndpointIdentity CreateUpnIdentity (string upnName);
static member CreateUpnIdentity : string -> System.ServiceModel.EndpointIdentity
Public Shared Function CreateUpnIdentity (upnName As String) As EndpointIdentity

Paramètres

upnName
String

Nom de l'identité UPN.

Retours

EndpointIdentity

EndpointIdentity UPN associée au upnName spécifié.

Exceptions

upnName a la valeur null.

Exemples

L'exemple de code suivant montre comment appeler cette méthode.

namespace TestPrincipalPermission
{
    class PrincipalPermissionModeWindows
    {

        [ServiceContract]
        interface ISecureService
        {
            [OperationContract]
            string Method1();
        }

        class SecureService : ISecureService
        {
            [PrincipalPermission(SecurityAction.Demand, Role = "everyone")]
            public string Method1()
            {
                return String.Format("Hello, \"{0}\"", Thread.CurrentPrincipal.Identity.Name);
            }
        }

        public void Run()
        {
            Uri serviceUri = new Uri(@"http://localhost:8006/Service");
            ServiceHost service = new ServiceHost(typeof(SecureService));
            service.AddServiceEndpoint(typeof(ISecureService), GetBinding(), serviceUri);
            service.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.UseAspNetRoles;
            service.Open();

            EndpointAddress sr = new EndpointAddress(
                serviceUri, EndpointIdentity.CreateUpnIdentity(WindowsIdentity.GetCurrent().Name));
            ChannelFactory<ISecureService> cf = new ChannelFactory<ISecureService>(GetBinding(), sr);
            ISecureService client = cf.CreateChannel();
            Console.WriteLine("Client received response from Method1: {0}", client.Method1());
            ((IChannel)client).Close();
            Console.ReadLine();
            service.Close();
        }

        public static Binding GetBinding()
        {
            WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message);
            binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            return binding;
        }
    }
}

Remarques

Un client WCF sécurisé qui se connecte à un point de terminaison avec cette identité utilise l’UPN lors de l’exécution de l’authentification SSPI avec le point de terminaison.

Cette méthode statique crée une instance de UpnEndpointIdentity en appelant son constructeur, UpnEndpointIdentity, en utilisant upnName en guise de paramètre d'entrée.

Si le upnName est spécifié avec une chaîne vide, l'authentification bascule de Kerberos vers NTLM, lorsque c'est possible. Si AllowNtlm c’est le cas false, WCF fait le meilleur effort pour lever une exception si NTLM est utilisé. Notez que l'affectation de la valeur false à cette propriété peut ne pas empêcher la transmission des informations d'identification NTLM.

S’applique à