Use isline framework to Develop ASP. NET programs-use session, cookie, and security support

Source: Internet
Author: User

This is the seriesArticleThe last article of isline. httpcontent. the cache support items in the httpcontentprovider namespace. This section describes the support for session and cookie in the namespace.

This is a common component in isline framework.ProgramProvides unified cookie configuration attributes, such as Cookie encryption, whether HTTPOnly, whether HTTPS security options are enabled, and the default scope. Cookieprovider is the class in the namespace. When the class is initialized, the Web is automatically loaded. the cookie node configuration in the config file will be loaded to the entire web site after successful reading. These attributes do not need to appear in the program.

Web. comfig node configuration:

Add the following configuration between </configsections> and <appsettings>:

<Isline. httpcontent. Configuration>
<Cookie>
<Cryptography> false </cryptography>
<Ishttponly> true </ishttponly>
<Issecure> false </issecure>
<Domain> default </domain>
</Cookie>
</Isline. httpcontent. Configuration>

Add the following nodes between <configsections> and </configsections>:

<Sectiongroup name = "isline. httpcontent. Configuration">
<Section name = "cookie" type = "isline. Data. configuration. suitconfig"/>
</Sectiongroup>

Then you can use this provider in the program. In the <isline. httpcontent. configuration> in the node, cryptography indicates whether to enable cookie encryption. It has two values: True and fasle. If the value is true, when creating or updating a cookie, the provider will automatically enable isline. security. the encrypting method in the cryptography namespace is encrypted. When the cookie is read, the provider automatically enables isline. security. decrypt the decrypting method in the cryptography namespace. security. the cryptography information will be introduced later. The seed string is used for encryption and decryption. The seed is added to Web. config For Configuration:

<Add key = "isline. Security. configuration. tokenkey" value = "isline"/>

After configuration, the value of the cryptography node can be set to true. In this case, the cookie information on the hard disk is encrypted.

Ishttponly corresponds to the HTTPOnly attribute of the cookie. This attribute indicates whether the user cookie can be obtained only on the server side. If the value is true, client methods such as JavaScript and flash cannot be obtained. This requires browser support for IE6 SP1 or later versions.

Issecure corresponds to the secure attribute of the cookie. If this attribute is true, the cookie can only be transmitted through https or other security protocols, which is invalid in HTTP. Setting the secure attribute does not mean that others cannot see the Cookies stored locally on your machine. It only ensures that the data transmission process between the cookie and the Web server is encrypted, while the cookie files stored locally are not encrypted. If you want to encrypt the Cookies stored locally, set the cryptography node to true.

Domain corresponds to the domain attribute of the cookie, which specifies the cookie scope. For example, there is a site www.abc.com, and then a site similar to isline.abc.com is added. In order to achieve cookie sharing for the web site, you need to reset the cookie domain, at this time, the domain value can be set to abc.com, so that all websites based on abc.com can share cookies.

This is the convenience of cookieprovider. You only need simple configuration to manage the whole site cookie!

Cookieprovider also implements the capability interface, but unlike the cache class, it only implements the ibasecapability interface:

Public class cookieprovider: ibasecapability
{
Public cookieprovider ()
{
}
}

Ibasecapability interface content:

Public interface ibasecapability
{
Bool remove (string name );
Bool isexit (string name );
}

To use this provider for Cookie operations, you must specify a cookie name. The default cookie does not exist, which is different from the cache. To use cookies, you need to instantiate the cookieprovider class and then use the instance for operations.

Cookieprovider supports the following methods:

● String getcontent: get Cookie content

● Bool updatecontent: Creates or updates a cookie. If the cookie does not exist, the provider automatically creates a new cookie. If you use the method with parameters, you can define the type of datetype to specify the expiration time, which can be hours, minutes, etc. Howlong indicates the specific number of the expiration time, which can be used together with datetype to determine the specific expiration time.

● Bool remove: removes all cookies under the site or deletes cookies with a certain prefix. cookieprefixname is the prefix and cookieprefixnamelength is the prefix length (count from 1 ); or input an arraylist to pass in the name of a set of cookies. removetype has two enumerated values: removetype. removeinput indicates to delete the cookie in the input arraylist array, removetype. removeallexcludeinput indicates that the cookie in the input arraylist array is retained, and other cookies in the site are deleted. You can also specify the cookie number or name.

● Bool isexit: determines whether the specified cookie exists and passes in the cookie name or number.

The following describes the session support in the namespace. This component can be used to provide unified session configuration attributes for the whole site program. It is easy to use, and the sessionprovider class also implements icapability, the advantage of using this provider is that it provides programmers with the same access method as accessing the cache and cookie.

Public class sessionprovider: icapability
{
Public sessionprovider ()
{
}
}

The usage method is the same as that of cookie and needs to be instantiated. However, you must note that the getcontent () method of sessionprovider and cookieprovider is returned in sessionprovider with the object type, return a string in cookieprovider. Because cookies are not serializable, only string-type variables can be stored in cookies. Session is serializable and session can store objects. Therefore, after you use getcontent () of sessionprovider to get the session, you also need to use the forced conversion method to obtain the required type. If you are interested in the relevant methods, you can visit the author's blog.

Httpcontentprovider is introduced here. Next we will introduce the last namespace, isline. Security. cryptography namespace.

This namespace completes the encryption and decryption process of the string type, which is completed by the class cryptography. There are many such methods. You can use the following common methods:

● Static string strengthencrypting: Performs secondary encryption based on the encrypting method. Des and Rijndael are used.AlgorithmAnd then move the string location according to the rules to implement encryption. This method is a strong encryption method. The parameter key is a seed

● Static string strengthdecrypting: used with the strong encryption method for decryption. The parameter key is the seed, and the seed must be consistent with the seed of the strong encryption method.

● Static string encrypting: encrypts an input string. The parameter key is the seed.

● Static string decrypting: used in combination with encrypting. It is the decryption method. The key is the seed, and the seed must be consistent with the seed of the encrypting method.

The following example shows how to use it:

First introduce the namespace: Using isline. Security. cryptography;

Use the followingCode:

String A = cryptography. strengthencrypting ("encrypted content", "isline") // strong encryption
Cryptography. strengthdecrypting (a, "isline"); // use strong encryption for decryption
String B = cryptography. encrypting ("encrypted content", "isline") // common Encryption
Cryptography. Decrypting (B, "isline"); // decrypt with common Encryption

This namespace is relatively simple to use and will not be further elaborated.

Isline framework is a framework that supports enterprise application system development. developers can quickly develop the application systems required by the Enterprise Based on the isline framework. The current version is v1.0.0.0. As demand continues to improve, the isline Framework version is also being upgraded.

The isline framework encapsulates data sources, server variables, caches, logs, exceptions, security and other elements. It supports multiple databases and multiple databases to operate simultaneously. It supports cross-method transactions; multiple log Renderer functions simultaneously, which means that developers can write classified log information to different targets at the same time. This framework is centered on configuration files. All changes (such as database changes and log recording media changes) can be completed by modifying the configuration file. The author's blog has a summary article on configuration, interested friends can be used as dictionaries. You are welcome to read them.

Now, all the namespaces of isline framework have been introduced, the DLL involved in this article can be downloaded from this: http://files.cnblogs.com/isline/IsLineFrameWorkDLL.rar

If you can settle down, master this framework, I believe that the efficiency of everyone will be improved, and if there are any mistakes in this article, welcome to visit the author's blog http://isline.cnblogs.com exchange guide, at the same time, I would like to thank the software news for providing me with an opportunity to meet readers.

This article has been published simultaneously in the software report and cannot be reproduced by any personal media.

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.