c# generate barcode from string Exercise 2: Adding a Custom Policy in Visual C#.NET

Drawer European Article Number 13 in Visual C#.NET Exercise 2: Adding a Custom Policy

Exercise 2: Adding a Custom Policy
EAN13 Maker In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create EAN-13 image in .NET framework applications.
www.OnBarcode.com
Scan EAN-13 Supplement 5 In Visual C#
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In this lab, you will create a custom policy to handle incoming messages that are dig itally singed. 1. Open the solution created in the previous exercise named DigitalSigPolicyLab. 2. Expand the App_Code folder in the Web service and select the Service.cs or Ser vice.vb file, depending on which language you are working in. At the top of the class, ensure that the following namespaces are included (using the using key word in C# or the Imports keyword in Visual Basic):
Bar Code Generator In C#.NET
Using Barcode generation for .NET Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Bar Code Scanner In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Microsoft.Web.Services3;
EAN 13 Generator In VS .NET
Using Barcode printer for ASP.NET Control to generate, create EAN-13 Supplement 5 image in ASP.NET applications.
www.OnBarcode.com
Generating GS1 - 13 In .NET Framework
Using Barcode printer for VS .NET Control to generate, create EAN / UCC - 13 image in .NET framework applications.
www.OnBarcode.com
System.Security.Principal;
EAN-13 Supplement 5 Generator In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create EAN-13 image in .NET framework applications.
www.OnBarcode.com
QR-Code Generation In C#.NET
Using Barcode generator for .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Lesson 3: Using Digital Signatures to Ensure Message Integrity
DataMatrix Maker In C#
Using Barcode creator for .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
Painting Code 128C In Visual C#
Using Barcode maker for .NET framework Control to generate, create Code-128 image in VS .NET applications.
www.OnBarcode.com
Microsoft.Web.Services3.Design;
Painting UPCA In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create UPC-A Supplement 2 image in .NET applications.
www.OnBarcode.com
USD - 8 Encoder In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create USD - 8 image in Visual Studio .NET applications.
www.OnBarcode.com
Microsoft.Web.Services3.Security;
Encoding Bar Code In Objective-C
Using Barcode creator for iPhone Control to generate, create bar code image in iPhone applications.
www.OnBarcode.com
ANSI/AIM Code 128 Printer In Java
Using Barcode generation for Java Control to generate, create Code 128A image in Java applications.
www.OnBarcode.com
Microsoft.Web.Services3.Security.Tokens;
Drawing DataMatrix In Objective-C
Using Barcode generator for iPad Control to generate, create Data Matrix 2d barcode image in iPad applications.
www.OnBarcode.com
QR-Code Encoder In Java
Using Barcode drawer for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
System.Security.Cryptography.X509Certificates;
Recognizing GTIN - 12 In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
GS1 - 12 Recognizer In Visual Basic .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
3. At the class declaration of the service, add the following line of code to specify a pol icy; this code should go under the WebService, WebServiceBinding, and DesignerGenerated attributes. (Only Visual Basic generates the DesignerGenerated attribute, so it is of no concern if you are using C#.)
Code 128 Code Set A Generator In None
Using Barcode printer for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com
Generating EAN-13 In Java
Using Barcode generator for Java Control to generate, create EAN-13 image in Java applications.
www.OnBarcode.com
'VB <Policy(GetType(DemoPolicy))> _ //C# [Policy(typeof(DemoPolicy))]
4. At the end of the class definition for the Web service, create another class that will define the policy. (This class could just as easily be added in a separate file, and there is no problem in doing so. However, for the sake of simplicity, add the class in the same file as the service because the service needs this class to run.) Add the following class definition after the end of the class definition of the ser vice:
'VB Public Class DemoPolicy
Inherits Policy
Public Const CERTIFICATE As String = "CERTIFICATE REFERENCE HERE" Public Sub New()
Dim DemoAssertion As UsernameForCertificateAssertion = _
New UsernameForCertificateAssertion()
DemoAssertion.X509TokenProvider = _ New X509TokenProvider( _ StoreLocation.LocalMachine, _ StoreName.My, _ CERTIFICATE, _ X509FindType.FindBySubjectDistinguishedName) Dim DemoProtection As EndpointProtectionRequirements = _ DemoAssertion.Protection Dim DemoOptions As SignatureOptions = _
SignatureOptions.IncludeAddressing Or _
SignatureOptions.IncludeTimestamp Or _
SignatureOptions.IncludeSoapBody
DemoProtection.Request.SignatureOptions = DemoOptions DemoProtection.Request.EncryptBody = True
9
Web Services Enhancements 3.0 in Client and Server Applications
DemoProtection.Response.SignatureOptions = DemoOptions DemoProtection.Response.EncryptBody = True DemoProtection.Fault.SignatureOptions = DemoOptions DemoProtection.Fault.EncryptBody = False DemoAssertion.MessageProtectionOrder = _ MessageProtectionOrder.SignBeforeEncrypt DemoAssertion.RequireDerivedKeys = True Me.Assertions.Add(DemoAssertion) End Sub End Class //C# public class DemoPolicy : Policy { private const String CERTIFICATE = "CERTIFICATE REFERENCE HERE"; public DemoPolicy() { UsernameForCertificateAssertion DemoAssertion = new UsernameForCertificateAssertion(); DemoAssertion.X509TokenProvider = new X509TokenProvider(StoreLocation.LocalMachine, StoreName.My, CERTIFICATE, X509FindType.FindBySubjectDistinguishedName); EndpointProtectionRequirements DemoProtection = DemoAssertion.Protection; DemoProtection.Request.SignatureOptions = SignatureOptions.IncludeAddressing | SignatureOptions.IncludeTimestamp | SignatureOptions.IncludeSoapBody; DemoProtection.Request.EncryptBody = true; DemoProtection.Response.SignatureOptions = SignatureOptions.IncludeAddressing | SignatureOptions.IncludeTimestamp | SignatureOptions.IncludeSoapBody; DemoProtection.Response.EncryptBody = true; DemoAssertion.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt; DemoAssertion.RequireDerivedKeys = true; this.Assertions.Add(DemoAssertion); } }
Lesson 3: Using Digital Signatures to Ensure Message Integrity
5. To enable a client to use this policy, all that needs to be done is to copy the class definition to a method in the client and specify that the policy be used. If the cli ent had a method named GetPolicy, it would look like the following:
'VB Private Function GetPolicy() As SecureInvoiceServiceWse Public Const CERTIFICATE As String = "CERTIFICATE REFERENCE HERE" Dim DemoAssertion As UsernameForCertificateAssertion = _ New UsernameForCertificateAssertion() DemoAssertion.X509TokenProvider = _ New X509TokenProvider( _ StoreLocation.LocalMachine, _ StoreName.My, _ CERTIFICATE, _ X509FindType.FindBySubjectDistinguishedName) Dim DemoProtection As EndpointProtectionRequirements = _ DemoAssertion.Protection Dim DemoOptions As SignatureOptions = _
SignatureOptions.IncludeAddressing Or _
SignatureOptions.IncludeTimestamp Or _
SignatureOptions.IncludeSoapBody
DemoProtection.Request.SignatureOptions = DemoOptions DemoProtection.Request.EncryptBody = True DemoProtection.Response.SignatureOptions = DemoOptions DemoProtection.Response.EncryptBody = True DemoProtection.Fault.SignatureOptions = DemoOptions DemoProtection.Fault.EncryptBody = False DemoAssertion.MessageProtectionOrder = _
MessageProtectionOrder.SignBeforeEncrypt
DemoAssertion.RequireDerivedKeys = True
Me.Assertions.Add(DemoAssertion) End Function //C# private Policy GetPolicy() {
private String CERTIFICATE = "CERTIFICATE REFERENCE HERE";
UsernameForCertificateAssertion DemoAssertion =
new UsernameForCertificateAssertion();
DemoAssertion.X509TokenProvider =
9
Web Services Enhancements 3.0 in Client and Server Applications
new X509TokenProvider(StoreLocation.LocalMachine, StoreName.My, CERTIFICATE, X509FindType.FindBySubjectDistinguishedName); EndpointProtectionRequirements DemoProtection = DemoAssertion.Protection; DemoProtection.Request.SignatureOptions =
SignatureOptions.IncludeAddressing |
SignatureOptions.IncludeTimestamp |
SignatureOptions.IncludeSoapBody;
DemoProtection.Request.EncryptBody = true; DemoProtection.Response.SignatureOptions =
SignatureOptions.IncludeAddressing |
SignatureOptions.IncludeTimestamp |
SignatureOptions.IncludeSoapBody;
DemoProtection.Response.EncryptBody = true;
DemoAssertion.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt; DemoAssertion.RequireDerivedKeys = true; this.Assertions.Add(DemoAssertion);
6. Now before any call to the service, all that needs to be done is setting the policy:
'VB Dim DemoPolicy As Policy = GetPolicy()
proxy.SetPolicy(DemoPolicy)
//C# Policy DemoPolicy = GetPolicy();
proxy.SetPolicy(DemoPolicy);
Lesson Summary
Digital signatures can be used to verify a message s integrity. Authentication deals with verifying the sender and receivers of a message. Confidentiality deals with ensuring that only approved entities can view a mes sage s content. Integrity deals with ensuring that a message has not been tampered with after it has been transmitted. Tokens that can be attached to a message include X.509, a UserNameToken, Ker beros tokens, and custom binary tokens. Tokens can be added to the Tokens collection of the RequestSoapContext of a WSE-enabled Web service or proxy.
Copyright © OnBarcode.com . All rights reserved.