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) {}}