More comprehensive Asp.net submission Verification Scheme (below)

Source: Internet
Author: User

Next.

The following describes how to extend the function of the generated "Submit Verification" class through. NetThe "partial Classification" or "Extension Method" technology can be easily implemented. Here, the "partial Classification" technology is used:

Reference generatedADO. NET Entity FrameworkThe namespace of the data model and declared as a partial classification.

Write a static constructor and a static attribute:

Static Submit for verification()

{

Expiration time difference=3;

}

 

/// <Summary>

/// Used to calculate the expiration time, in minutes

/// </Summary>

Public StaticDouble Expiration time difference

{

Get

{

Return_Expiration time difference;

}

Set

{

_Expiration time difference=Value;

}

}

Private StaticDouble_Expiration time difference;

How to add verification information:

/// <Summary>

///Add a new verification information. Note thatSessionAny variable value in; otherwiseSessionidWill change randomly and cannot pass Verification

/// </Summary>

/// <Param name ="Verification Code">Verification code to save</Param>

/// <Returns>Verification InformationID</Returns>

Public StaticGuid Add(StringVerification Code)

{

VaRA= New Submit for verification

{

ID= Guid.Newguid (),

SessionID= Httpcontext.Current.Session.Sessionid,

Submitted? =False,

Verification Code =Verification Code,

Expiration time =Datetime.Now.Addminutes (Expiration time difference)

};

Using(CommondbentitiesC=New Commondbentities())

{

C.AddtoSubmit for verification();

C.Savechanges ();

}

ReturnA.ID;

}

This method returns the added verification informationGuidBefore executing this method, you mustSessionAssigned value. If not assigned valueSessionidIt will be random, which will make the subsequent verification function think that the client is hijacked.

How to obtain verification information:

/// <Summary>

///PassIDObtain verification information

/// </Summary>

/// <Param name = "ID">Verification InformationID</Param>

/// <Returns>Verification Information</Returns>

Public StaticSubmit for verification Obtain(Guid ID)

{

Try

{

Submit for verificationA;

Using(CommondbentitiesC= New Commondbentities())

{

A=C.Submit for verification.First (F=> F.ID=ID );

}

ReturnA;

}

Catch{Return Null;}

}

To verify the information submitted by a user, follow these steps:

/// <Summary>

///Verify that the verification code entered by the user is correct

/// </Summary>

/// <Param name = "ID">Verification InformationID</Param>

/// <Param name ="Verification Code">User-entered Verification Code</Param>

/// <Returns>An error message is returned. If the verification is successful, an error message is returned.Null</Returns>

Public StaticString Verify(Guid ID,String Verification Code)

{

VaR Verification Information =Submit for verification.Obtain(ID );

If(Verification Information =Null)Return "The verification information is invalid or has expired.";

Else If(Verification Information.Expiration time <Datetime.Now)Return "Verification Information expired";

else If ( Verification Information . submitted ) return " information submitted " ;

else If ( Verification Information . session id . trim () ! = httpcontext . current . session . sessionid) return " Illegal Hijacking of Verification Information " ;

Else If(Verification Information.Verification Code.Trim (). Tolower ()! = Verification Code.Tolower ())Return"Incorrect verification code";

Else Return Null;

}

Methods for marking submitted information and clearing timeout information:

/// <Summary>

///SpecifyIDSet verification information to submitted

/// </Summary>

/// <Param name = "ID">Verification InformationID</Param>

Public StaticVoid Set to submitted(Guid ID)

{

Using(CommondbentitiesC= New Commondbentities())

{

VaRA=C.Submit for verification.First (F=> F.ID=ID );

A.Submitted? =True;

C.Savechanges ();

}

Clear(False );

}

 

/// <Summary>

///Clear invalid old data in the database

/// </Summary>

/// <Param name ="Clear submitted data?">Indicates whether to clean up the submitted data. Otherwise, only the expired data is cleared.</Param>

/// <Returns>Total affected data volume</Returns>

Public StaticInt Clear(BoolClear submitted data?)

{

IntX= 0;

Using(CommondbentitiesC= New Commondbentities())

{

VaRA=C.Submit for verification.Where (F=> F.Expiration time <Datetime.Now|(Clear submitted data? ? F.Submitted?:False ));

Foreach(Submit for verificationFIn)

{

C.Deleteobject (f );

}

X=C.Savechanges ();

}

ReturnX;

}

In the set submitted method, clear the timeout information by hand.

Now, the verification class is compiled.

Next let's take a look at how to use it. First, create a page like this:

As described above, verification informationIDWill be sent to the client in plaintext, that is, stored inHiddenfieldControl.

CustomvalidatorDisplays error messages.

The following is the pageLoadEventCode:

Note that you have set this parameter before calling the function for adding verification information.SessionMake sure thatSessionidIt will not change.

The code for generating verification images is everywhere, so it is not mentioned here, as long as you passURLParameter"ID"GetGuidTo obtain the corresponding verification code.

The code for page submission is as follows:

Application results:

Database Data:

This article is over now. I hope to help you. If you have a better solution, please feel free to discuss it here.

Download the PDF version of this article: http://www.box.net/shared/dtisa6mik8

Related Article

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.