The HTTPContext 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 {///
Public myprincipal (string userid, string password) {// // Todo: Add constructor logic // identity = new myident; if (Identity.isauthenticated) {// Get it if verified The user's role, which can be modified to read the specified user's role and add it to the Role. This example adds an admin role RoleList = new arraylist (); rolelist.add ("admin ");} Else {// do nothing}} public arraylist rolelist;}} #Region iPrIncipal member
Public system.security.principal.iidentity Identity {get {// Todo: Add myprincipal.Identity getter implementation returnidity;} set {identity = value;}}
Public Bool Isinrole (String Role) {// Todo: Add myprincipal.isinrole Implement Return RoleList.Contains (role) ;;}
#ndregion}}}
Myidentity.cs
Using system;
Summary description of Namespace httpContextusereg {///
Private bool canpass () {/ / The friends here can change to verify the username and password from the database according to their own needs, // Here I can specify the string IF you specified directly (userid == "YAN0lovesha" && password == " "} else {returnaf;}}}
Public string password {get {return password;} set {password = value;}}
#Region IIDENTITY member
Public Bool isauthenticated {get {// Todo: Add myidentity.isauthenticated getter Implement Return canpass ();}}
PUBLIC STRING NAME {Get {// Todo: Add myidentity.name getter Implement Return UserID;}} // This property We can use it according to your needs, in this case, there is no use of 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;
A summary description of Namespace httpContextusereg {///
Protected Override Void OnInit (Eventargs E) {Base.onit (E); this.Load = New EventHandler (mypage_load);}
// Extract user information from the cache when the page is loaded, the user information private void mypage_load (ipxt.user.Identity.isAuthentic) {if (Context.cache ["UserMessage"]! = NULL ) {Hashtable userMessage = (Hashtable) Context.Cache [ "UserMessage"];. MyPrincipal principal = new MyPrincipal (userMessage [ "UserID"] ToString (), userMessage [ "UserPassword"] ToString ());. Context.User = The following 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 ">