Transfer from: http://blog.9cbs.net/yan0lovesha/archive/2005/01/13/252508.aspxhttpContext class contains all specific HTTP information for individual HTTP requests. This example is mainly how to use the user attribute in the HTTPContext class to implement user authentication!
User authentication is that most ASP.NET web applications are used, which posses a very important status throughout the application. In .NET, there are many user authentication methods, such as well-known Passport authentication, Windows certification , Form certification, etc., it is difficult to meet our needs in practical applications, so that many friends are all written by themselves to implement their own features, which makes us consideration in security and system efficiency. .
In fact, the user verification mechanism built in the ASP.NET is very powerful, and it also has very good scalability, which can generate an attribute called User in the HTTPContext object. This property allows us to access various information. , Including whether the user has verified, the user's type, user name, etc. We can also expand the functionality of this property to achieve our requirements.
Objects assigned to HTTPContext.user must implement an IPRINCIPAL interface, one of the IPrInCIPAL defined attributes is Identity, which must implement the IIDENTITY interface. Because we only need to write classes that implement these two interfaces, we can add any of these classes we need.
First, we create two classes that implement iPrincipal and IIDENTITY, divided by another Myiprincipal and MyIndentity
Myiprincipal.cs
Using system;
Using system.collections;
Namespace httpcontextusereg
{
///
/// myPrincipal's summary description.
/// summary>
// / Implement an IPRINCIPAL interface
Public class myprincipal: system.security.principal.iprincipal
{
Private system.security.principal.iziDentity Identity;
Private arraylist rolelist;
Public myprincipal (String Userid, String Password)
{
//
// TODO: Add constructor logic here
//
Identity = New MyIdentity (userID, password);
Ide (Identity.isauthenticated)
{
// If you get the user's Role, you can modify it from the database.
/ / Read the specified user's role and add it to the Role, add an admin role to the user directly in this example
RoleList = new arraylist ();
RoleList.Add ("admin");
}
Else
{
// do nothing
}
}
Public ArrayList RoleList
{
get
{
Return RoleList;
}
}
#Region iPrincipal member
Public system.security.principal.iziDentity Identity
{
get
{
// Todo: Add myprincipal.Identity getter implementation
Return Identity;
}
set
{
Identity = value;}
}
Public Bool Isinrole (String Role)
{
// Todo: Add myprincipal.isinrole implementation
Return RoleList.Contains (Role) ;;
}
#ndregion
}
}
Myidentity.cs
Using system;
Namespace httpcontextusereg
{
///
/// myidentity's summary description.
/// summary>
// / Implement the IIDENTITY interface
Public class myidentity: system.security.principal.iidentity
{
Private string userid;
PRIVATE STRING Password;
Public MyIdentity (String CurrentUserid, String CurrentPassword)
{
//
// TODO: Add constructor logic here
//
Userid = CurrentUserId;
Password = currentpassword;
}
Private bool canpass ()
{
/ / The friends here can change the user name and password from the database according to their needs.
/ / Here, I can easily specify the string directly specified.
IF (userid == "yan0lovesha" && password == "ioveshasha")
{
Return True;
}
Else
{
Return False;
}
}
Public String Password
{
get
{
Return Password;
}
set
{
Password = Value;
}
}
#Region IIDENTITY member
Public Bool Isauthenticated
{
get
{
// Todo: Add myidentity.isauthenticate getter implementation
Return canpass ();
}
}
Public String Name
{
get
{
// Todo: Add myidentity.name getter implementation
Return UserId;
}
}
// This property we can use according to your needs, it is not used in this example.
Public String AuthenticationType
{
get
{
// Todo: Add myidentity.AuthenticationType getter implementation
Return NULL;
}
}
#ndregion
}
}
After completing these two classes, we also have to create a self-proven, here we are named mypage, inheriting from page classes.
Mypage.cs
Using system;
Using system.collections;
Namespace httpcontextusereg
{
///
A summary description of /// mypage.
/// summary>
/// Inherited from Page class
Public class mypage: system.web.ui.page
{
Public mypage ()
{
//
// TODO: Add constructor logic here
//
}
Protected Override Void OnNit (Eventargs E)
{
Base.onit (E);
This.Load = New EventHandler (mypage_load);
}
// Extract user information from the cache when loading
Private void mypage_load (Object Sender, System.EventArgs E)
{
IF (context.user.Identity.isauthenticated)
{
IF (Context.Cache ["UserMessage"]! = null)
{
Hashtable UserMessage = (Hashtable) Context.cache ["UserMessage"];
MyPrincipal Principal = New MyPrincipal (UserMessage ["Userid"]. TOSTRING (), userMessage ["userpassword"]. TOSTRING ());
CONTEXT.USER = Principal;
}
}
}
}
}
Below is our interface WebForm.aspx and WebForm.aspx.cs
WebForm.aspx
<% @ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "httpContextusereg.webform1"%>%>
3C
// DTD HTML 4.0 Transitional // En ">
HEAD>