ASP. NET security-Asp. Net Security Architecture-how to implement. Net Security)

Source: Internet
Author: User

ASP. NET security architecture-how to implement. Net Security

Conversion from http://www.cnblogs.com/yanyangtian/archive/2009/05/02/1447753.html)

Are you often confused by many concepts when using forms verification? Do you really understand what is principal, identity, and iprincipan ...? Many documents seldom mention what these items are. They are just about how to use them, and the results are problematic. As a result, many friends simply stop at the surface and use them as a result. I believe there will be some gains after reading this article.

The ASP. Net security architecture provides an object model for implementing the security mode of Web applications. No matter which authentication mode we choose, many of the elements are the same. Users logging on to the application are granted principal and identity according to the creden。 they provide. The principal object indicates the current security context of the user, including the user's identity and roles. The identity object indicates the current user. The principal object is created using the identity object (indicating the user's identity), and it adds some additional information, such as role or custom data.

In short: Principal = identity + role + Custom Data

One thing you should note: Identity Authentication occurs in a specific period of ASP. NET running. Remember this and I will talk about it later. Let's talk about it as follows:

1. Security Context
The identity object indicates the authenticated user. The type of the Identity object depends on the authentication in use. For example, Windows Authentication uses the windowsidentity object, while froms authentication uses the formsidentity object.

It is a little uncomfortable to get started with the concept of identity. In fact, identity is a user's identity. What is the identity? It indicates what the user is and what the user name is. It's just a little more professional here.
 
In addition, the principal object indicates the members of the group or role of the user who passes Identity Authentication: that is, the security context of the current user. In security context, the white point is the information that this object contains many user identities. The principal object is automatically created for Windows Authentication in IIS, but we can also create a common principal object (which you will understand later ).
During programming, you have used the httpcontent. currrent. User attribute more or less. In fact, it represents a principal object. The principal object implements the iprincipal interface.
 
 
Iprincipal Interface
Different authentication modes have different security context requirements. We can use the principal object to represent the current security context. The iprincipal interface defines the basic functions of the principal object. You can also customize the security context object as long as the system. Security. Principal interface is implemented:


Identity attribute -- you can obtain the identity of the current principal object. As mentioned earlier, Principal contains identity.
Isinrole (string rolename) method-you can determine whether the current principal object belongs to a specified role. You have also used a similar httpcontent. Current. User. Identity. isinrole ("admin") statement.
 
The principal object can be accessed through the httpcontent. Current. User attribute. The following code should be used:

If (httpcontext. Current. User. Identity. isauthenticated)
{
Lblusername. Text = httpcontext. Current. User. Identity. Name + "logged on ";
}

(Note: identity is the user ID, including the user name. As we will talk about later)

The following code is more common: determine whether the current user is an administrator role.

If (httpcontext. Current. User. isinrole ("admin ")
{
//
}

 

Next, let's take a look at the object that implements the iprincipal interface built in ASP. NET:
 
Genericprincipal class
 
The genericprincipal class implements the iprincipal interface. From the name, we can see that the genericprincipal object represents a general and basic security context. It only defines the role of the current user. That is to say, this object only partially implements the iprincipal interface. (In the future, we will call the object implementing the iprincipal interface as the subject ). For a Windows Authentication mode, windowsprincipal is used, because windowsprincipal implements iprincipal more specifically. In forms authentication, only genericprincipal is used. That is to say, we can implement custom principal objects according to our requirements. As mentioned below.
 
Every implementation of the iprincipal interface must rewrite the identity attribute and isinrole method. The isinrole method of the genericprincipal class compares the role value with the role defined in the string, the isinrole method of the windowsprincipal class compares the role with the role assigned to the Windows user account.

We can create an instance of the genericprincipal class to be used throughout the lifecycle of the current request, and assign it to the httpcontent. Current. User attribute.
The genericprincipal constructor has two parameters: the user's genericidentity (the user identity genericidentity implements the iidentity Interface) and a string array that represents the user role. So we previously said: Principal = identity + role is the reason.
Once a genericprincipal object is created, it can be assigned to the httpcontent. Current. User attribute to indicate the security context of the currently requested user.
 
The following is an example of the created code:

Code
// Create a general genericprincipal
// We have said: the identifier is the object containing the user name. The following contains an identifier named "Xiaoyang ".
Genericidentity identity = new genericidentity ("Xiaoyang ");
 
// Create genericprincipal
// Note roles indicates a character string array of a role, such as role = new string {"admin", "customer "};
Genericprincipal principal = new genericprincipal (identity, roles );

// Add
Httpcontext. Current. User = principal;

 
Note: The above code should be written in a specific place, that is, the specific time of the lifecycle, which will be described later.
 
After talking about Principal, let's talk about what the user identity is.
 
2. User ID

The identity object is used to identify the current user. The identifier can only provide a small amount of security context information, such as the user name. The identity object can be used to authenticate users.
 
Iidentity Interface
Like the iprincipal interface, this interface must be implemented for all objects identified by the user. The iidentity interface defines the structure of the basic amount of the Identity object, which is defined as follows:

Authenticationtype (string type) attribute -- it can obtain the type of identity authentication used. For example, if forms authentication is used, the property returns a "forms" string, therefore, the custom identifier can return the "mimidentity" string.
Isauthenticated (bool type) attribute -- identifies whether a user passes authentication. We can often use httpcontext. Current. User. Identity. isauthenticated to determine whether the user has logged on.
Name (string type) attribute -- obtains the user name. I believe it is no stranger to httpcontext. Current. User. Identity. Name.

Next, let's take a look at our own identity class that implements the iidentity interface.

 

Code

Using system;
Using system. Security. Principal;

Public class customidentity: iidentity
{
Private string name;
// The constructor only receives one string parameter. You can refer to the previous Code: genericidentity identity = new genericidentity ("Xiaoyang ");
Public customidentity (string name)
{
This. Name = Name;
}

//
Private string authenticatetype = "customeridentity ";
Public customidentity (string name, string authenticatetype)
{
This. Name = Name;
This. authenticatetype = authenticatetype;
}

// The following interface is implemented:
Private bool isauthenticated = false;
Public bool isauthenticated
{
Get {return isauthenticated ;}
}

Private string name;
Public string name
{
Get {return name ;}
}

}

The above code is just a demonstration, and you can expand according to your own requirements.
 
 
As before, let's take a look at the built-in identity class in ASP. NET:
Formsidentity -- Used in Forms authentication
Passwordidentity -- Used in passport verification
Genericidentity -- a common identifier
Windowsidentity -- used for Windows Authentication
 
Let's take a look at the use of genericidentity, and other usage.
 

In fact, genericidentity identifies a basic identity object. It is basic for identity objects. We have seen an example of genericprincipal before. In this example, we create an instance of the genericidentity class,

Genericidentity identity = new genericidentity ("Xiaoyang ");

 
We can also provide more specific identity objects, such as formsidentity mentioned earlier, to provide specific user information.
 

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.