Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Puede establecer el nivel de protección al aplicar un atributo adecuado y al configurar la propiedad. Puede establecer la protección en el nivel de servicio para que afecte a todas las partes de cada mensaje, o bien puede establecer la protección en niveles cada vez más granulares, desde métodos a elementos de mensaje. Para obtener más información sobre la propiedad , vea Descripción del ProtectionLevelnivel de protección.
Nota:
Solo puede establecer niveles de protección en el código, no en la configuración.
Para firmar todos los mensajes de un servicio
Cree una interfaz para el servicio.
Aplique el atributo ServiceContractAttribute al servicio y establezca la propiedad ProtectionLevel en Sign, como se muestra en el siguiente código (el nivel predeterminado es EncryptAndSign).
// Set the ProtectionLevel on the whole service to Sign. [ServiceContract(ProtectionLevel = ProtectionLevel.Sign)] public interface ICalculator' Set the ProtectionLevel on the whole service to Sign. <ServiceContract(ProtectionLevel:=ProtectionLevel.Sign)> _ Public Interface ICalculator
Para firmar todos los elementos de mensaje de una operación
Cree una interfaz para el servicio y aplique el ServiceContractAttribute atributo a la interfaz.
Agregue una declaración de método a la interfaz .
Aplique el atributo OperationContractAttribute al método y establezca en la propiedad ProtectionLevel a Sign, como se muestra en el código siguiente.
// Set the ProtectionLevel on the whole service to Sign. [ServiceContract(ProtectionLevel = ProtectionLevel.Sign)] public interface ICalculator { // Set the ProtectionLevel on this operation to None. [OperationContract(ProtectionLevel = ProtectionLevel.Sign)] double Add(double a, double b); }' Set the ProtectionLevel on the whole service to Sign. <ServiceContract(ProtectionLevel:=ProtectionLevel.Sign)> _ Public Interface ICalculator ' Set the ProtectionLevel on this operation to Sign. <OperationContract(ProtectionLevel:=ProtectionLevel.Sign)> _ Function Add(ByVal a As Double, ByVal b As Double) As Double End Interface
Protección de mensajes de error
Las excepciones que se producen en un servicio se pueden enviar a un cliente como fallos SOAP. Para obtener más información sobre cómo crear errores fuertemente tipados, vea Especificar y controlar errores en contratos y servicios y Cómo: Declarar errores en contratos de servicio.
Para proteger un mensaje de error
Cree un tipo que represente el mensaje de error. En el ejemplo siguiente se crea una clase denominada
MathFaultcon dos campos.Aplique el DataContractAttribute atributo al tipo y un DataMemberAttribute atributo a cada campo que se debe serializar, como se muestra en el código siguiente.
[DataContract] public class MathFault { [DataMember] public string operation; [DataMember] public string description; }<DataContract()> _ Public Class MathFault <DataMember()> _ Public operation As String <DataMember()> _ Public description As String End ClassEn la interfaz que devolverá el error, aplique el FaultContractAttribute atributo al método que devolverá el error y establezca el
detailTypeparámetro en el tipo de la clase de error.Además, en el constructor, establezca la propiedad ProtectionLevel a EncryptAndSign, como se muestra en el código siguiente.
public interface ICalculator { // Set the ProtectionLevel on a FaultContractAttribute. [OperationContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] [FaultContract( typeof(MathFault), Action = @"http://localhost/Add", Name = "AddFault", Namespace = "http://contoso.com", ProtectionLevel = ProtectionLevel.EncryptAndSign)] double Add(double a, double b); }Public Interface ICalculator ' Set the ProtectionLevel on a FaultContractAttribute. <OperationContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign), _ FaultContract(GetType(MathFault), ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _ Function Add(ByVal a As Double, ByVal b As Double) As Double End Interface
Protección de elementos de mensaje
Use un contrato de mensaje para proteger partes de un mensaje. Para obtener más información sobre los contratos de mensajes, vea Using Message Contracts.
Para proteger el cuerpo del mensaje
Cree un tipo que represente el mensaje. En el ejemplo siguiente se crea una
Companyclase con dos campos yCompanyNameCompanyID.Aplique el atributo MessageContractAttribute a la clase y establezca la propiedad ProtectionLevel a EncryptAndSign.
Aplique el atributo MessageHeaderAttribute a un campo que se expresará como encabezado de mensaje y establezca la propiedad
ProtectionLevelen EncryptAndSign.Aplique el MessageBodyMemberAttribute a cualquier campo que se expresará como parte del cuerpo del mensaje y establezca la propiedad
ProtectionLevelen EncryptAndSign, como se muestra en el ejemplo siguiente.[MessageContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] public class Company { [MessageHeader(ProtectionLevel = ProtectionLevel.Sign)] public string CompanyName; [MessageBodyMember(ProtectionLevel = ProtectionLevel.EncryptAndSign)] public string CompanyID; }<MessageContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _ Public Class Company <MessageHeader(ProtectionLevel:=ProtectionLevel.Sign)> _ Public CompanyName As String <MessageBodyMember(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _ Public CompanyID As String End Class
Ejemplo
En el ejemplo siguiente se establece la ProtectionLevel propiedad de varias clases de atributo en varios lugares de un servicio.
[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface ICalculator
{
[OperationContract(ProtectionLevel = ProtectionLevel.Sign)]
double Add(double a, double b);
[OperationContract()]
[FaultContract(typeof(MathFault),
ProtectionLevel = ProtectionLevel.EncryptAndSign)]
double Subtract(double a, double b);
[OperationContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
Company GetCompanyInfo();
}
[DataContract]
public class MathFault
{
[DataMember]
public string operation;
[DataMember]
public string description;
}
[MessageContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public class Company
{
[MessageHeader(ProtectionLevel = ProtectionLevel.Sign)]
public string CompanyName;
[MessageBodyMember(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public string CompanyID;
}
[MessageContract(ProtectionLevel = ProtectionLevel.Sign)]
public class Calculator : ICalculator
{
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double a, double b)
{
return a - b;
}
public Company GetCompanyInfo()
{
Company co = new Company();
co.CompanyName = "www.cohowinery.com";
return co;
}
}
public class Test
{
static void Main()
{
Test t = new Test();
try
{
t.Run();
}
catch (System.InvalidOperationException inv)
{
Console.WriteLine(inv.Message);
}
}
private void Run()
{
// Create a binding and set the security mode to Message.
WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Message;
Type contractType = typeof(ICalculator);
Type implementedContract = typeof(Calculator);
Uri baseAddress = new Uri("http://localhost:8044/base");
// Create the ServiceHost and add an endpoint.
ServiceHost sh = new ServiceHost(implementedContract, baseAddress);
sh.AddServiceEndpoint(contractType, b, "Calculator");
ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
sm.HttpGetEnabled = true;
sh.Description.Behaviors.Add(sm);
sh.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.CurrentUser,
StoreName.My,
X509FindType.FindByIssuerName,
"ValidCertificateIssuer");
foreach (ServiceEndpoint se in sh.Description.Endpoints)
{
ContractDescription cd = se.Contract;
Console.WriteLine($"\nContractDescription: ProtectionLevel = {cd.Name}");
foreach (OperationDescription od in cd.Operations)
{
Console.WriteLine($"\nOperationDescription: Name = {od.Name}");
Console.WriteLine($"ProtectionLevel = {od.ProtectionLevel}");
foreach (MessageDescription m in od.Messages)
{
Console.WriteLine($"\t {m.Action}: {m.ProtectionLevel}");
foreach (MessageHeaderDescription mh in m.Headers)
{
Console.WriteLine($"\t\t {mh.Name}: {mh.ProtectionLevel}");
foreach (MessagePropertyDescription mp in m.Properties)
{
Console.WriteLine($"{mp.Name}: {mp.ProtectionLevel}");
}
}
}
}
}
sh.Open();
Console.WriteLine("Listening");
Console.ReadLine();
sh.Close();
}
}
<ServiceContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Public Interface ICalculator
<OperationContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Function Add(ByVal a As Double, ByVal b As Double) As Double
<OperationContract(), _
FaultContract _
(GetType(MathFault), _
Action:="http://localhost/Add", _
Name:="AddFault", _
Namespace:="http://contoso.com", _
ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Function Subtract(ByVal a As Double, ByVal b As Double) As Double
<OperationContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Function GetCompanyInfo() As Company
End Interface
<DataContract()> _
Public Class MathFault
<DataMember()> _
Public operation As String
<DataMember()> _
Public description As String
End Class
<MessageContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Public Class Company
<MessageHeader(ProtectionLevel:=ProtectionLevel.Sign)> _
Public CompanyName As String
<MessageBodyMember(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Public CompanyID As String
End Class
<MessageContract(ProtectionLevel:=ProtectionLevel.Sign)> _
Public Class Calculator
Implements ICalculator
Public Function Add(ByVal a As Double, ByVal b As Double) As Double _
Implements ICalculator.Add
Return a + b
End Function
Public Function Subtract(ByVal a As Double, ByVal b As Double) As Double _
Implements ICalculator.Subtract
Return a - b
End Function
Public Function GetCompanyInfo() As Company Implements ICalculator.GetCompanyInfo
Dim co As New Company()
co.CompanyName = "www.cohowinery.com"
Return co
End Function
End Class
Public Class Test
Shared Sub Main()
Dim t As New Test()
Try
t.Run()
Catch inv As System.InvalidOperationException
Console.WriteLine(inv.Message)
End Try
End Sub
Private Sub Run()
' Create a binding and set the security mode to Message.
Dim b As New WSHttpBinding()
b.Security.Mode = SecurityMode.Message
Dim contractType As Type = GetType(ICalculator)
Dim implementedContract As Type = GetType(Calculator)
Dim baseAddress As New Uri("http://localhost:8044/base")
' Create the ServiceHost and add an endpoint.
Dim sh As New ServiceHost(implementedContract, baseAddress)
sh.AddServiceEndpoint(contractType, b, "Calculator")
Dim sm As New ServiceMetadataBehavior()
sm.HttpGetEnabled = True
sh.Description.Behaviors.Add(sm)
sh.Credentials.ServiceCertificate.SetCertificate( _
StoreLocation.CurrentUser, StoreName.My, _
X509FindType.FindByIssuerName, "ValidCertificateIssuer")
Dim se As ServiceEndpoint
For Each se In sh.Description.Endpoints
Dim cd As ContractDescription = se.Contract
Console.WriteLine(vbLf + "ContractDescription: ProtectionLevel = {0}", _
cd.Name, cd.ProtectionLevel)
Dim od As OperationDescription
For Each od In cd.Operations
Console.WriteLine(vbLf + "OperationDescription: Name = {0}", od.Name, od.ProtectionLevel)
Console.WriteLine("ProtectionLevel = {1}", od.Name, od.ProtectionLevel)
Dim m As MessageDescription
For Each m In od.Messages
Console.WriteLine(vbTab + " {0}: {1}", m.Action, m.ProtectionLevel)
Dim mh As MessageHeaderDescription
For Each mh In m.Headers
Console.WriteLine(vbTab + vbTab + " {0}: {1}", mh.Name, mh.ProtectionLevel)
Dim mp As MessagePropertyDescription
For Each mp In m.Properties
Console.WriteLine("{0}: {1}", mp.Name, mp.ProtectionLevel)
Next mp
Next mh
Next m
Next od
Next se
sh.Open()
Console.WriteLine("Listening")
Console.ReadLine()
sh.Close()
End Sub
End Class
Compilar el código
En el código siguiente se muestran los espacios de nombres necesarios para compilar el código de ejemplo.
using System;
using System.ServiceModel;
using System.Net.Security;
using System.ServiceModel.Description;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.Serialization;
Imports System.ServiceModel
Imports System.Net.Security
Imports System.ServiceModel.Description
Imports System.Security.Cryptography.X509Certificates
Imports System.Runtime.Serialization