WebService Development (ii) How to use SOAP extensions

Source: Internet
Author: User
Tags abstract decrypt soap

Soap Extension (SOAP extensions) can encapsulate reusable functionality. For example, the previous article is about access control for services through the SOAP header. In this essay we will use the SOAP extension to do the encapsulation of verifying the user's identity with the SOAP header and to solve the problem of unsafe transmission of identity information by netizens.

First, introduce a few related classes. The System.Web.Services.Protocols namespace under Soapextension,soapextensionattribute is two abstract classes that cannot be instantiated, and what we do is inherit the two abstract classes and implement the custom class separately. SoapExtensionAttribute also inherited from System.Attribute, that it is an attribute (this thing is not very good translation, someone translated into attributes, etc., I think about it, feel translated into the label more image, for what?) When we use this kind of class, we usually add [Attribute] above the class or method, which is a bit like labeling a thing and telling others what it is and what it is for.

We started implementing custom SoapExtension and SoapExtensionAttribute, with the following code, symmetric encryption in the code, and I encapsulated one of the implementations provided in MSDN. Namespaces that need to be referenced, including the namespace in which the cryptographic classes I encapsulate

Using System;

Using System.Data;

Using System.Configuration;

Using System.Web;

Using System.Web.Services.Protocols;

Using David.Fan.Encrypt;

The bottom is the custom Myextensionattribute class, and AttributeTargets.Method in [AttributeUsage (AttributeTargets.Method)] can only be applied to the method, Of course, you can also set other application types according to the different functions of the SOAP extension.

[AttributeUsage (AttributeTargets.Method)]

public class Myextensionattribute:soapextensionattribute

{

int _priority = 1;


public override int Priority

{get

{return _priority;}

set {_priority = value;}

Public


override Type ExtensionType

{get

{return typeof (Myextension);}

}

}

Next is the Myextension class, inherited from SoapExtension

The public class Myextension:soapextension {//This override method will be invoked four times//respectively SoapMessageStage Beforeserialize,afterse Rialize,beforedeserialize,afterdeserialize public override void ProcessMessage (SoapMessage message) {i F (message.

            Stage = = soapmessagestage.afterdeserialize)//deserialized after deserialization processing {bool check = false; foreach (SoapHeader header in message. Headers) {if (header is Mysoapheader) {Mysoapheader

                    MyHeader = (mysoapheader) header;

                    Decryption Myheader.name = Security.decrypt (myheader.name);


                    Myheader.password = Security.decrypt (Myheader.password); if (myheader.name = "Admin" | | | myheader.password = = "Admin") {check = True

                        ;

                    Break

      }} if (!check)          throw new SoapHeaderException ("Authentication Failure", Soapexception.clientfaultcode);

    } public override Object GetInitializer (type type) {return GetType (); public override Object GetInitializer (logicalmethodinfo info, SoapExtensionAttribute attribute) {RET

    Urn null; public override void Initialize (Object initializer) {}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.