How to Implement counters

Source: Internet
Author: User
Enter the following code in the global. asax. CS file: protected void application_start (Object sender, eventargs E)
{
Sqlconnection con;
Sqlcommand cmd;

// Get the connection string from the existing key in Web. config
Con = new sqlconnection (configurationsettings. etettings ["cnfriends. connectionstring"]);
Cmd = new sqlcommand ("select visitors from counter", con );
Con. open ();
Protected void session_start (Object sender, eventargs E)
{
Application. Lock ();
Application ["counter"] = (INT) application ["counter"]) + 1;
Application. Unlock ();
}
Try
{
// Retrieve the counter
Application ["counter"] = (INT) cmd. executescalar ();
}
Finally
{
Con. Close ();
}
}
Protected void application_authenticaterequest (Object sender, eventargs E)
{
// Cast the sender to the application
Httpapplication APP = (httpapplication) sender;

// Only replace the context if it has already been handled
// By Forms authentication module (user is authenticated)
If (App. Request. isauthenticated)
{
Sqlconnection con;
String SQL;
Sqlcommand cmd;

String id = context. User. Identity. Name;

Con = new sqlconnection (configurationsettings. etettings ["cnfriends. connectionstring"]);
SQL = "select isadministrator from [user] Where userid = '{0 }'";
SQL = string. Format (SQL, ID );
Cmd = new sqlcommand (SQL, con );
Con. open ();

// Ensure closing the connection
Try
{
Object admin = cmd. executescalar ();

// Was it a valid userid?
If (Admin! = NULL)
{
Genericprincipal ppal;
String [] roles;

// If isadministrator field is true, add both roles
If (bool) Admin) = true)
{
Roles = new string [] {"user", "admin "};
}
Else
{
Roles = new string [] {"user "};
}

Ppal = new genericprincipal (context. User. Identity, roles );
Context. User = ppal;
}
Else
{
// If userid was invalid, clear the context so he logs on again
Context. User = NULL;
}
}
Catch
{
Throw;
}
Finally
{
Con. Close ();
}
}

}
Protected void application_end (Object sender, eventargs E)
{
Sqlconnection con;
Sqlcommand cmd;

// Get the connection string from the existing key in Web. config
Con = new sqlconnection (configurationsettings. etettings ["cnfriends. connectionstring"]);
Cmd = new sqlcommand ("update counter set visitors =" + application ["counter"]. tostring (), con );
Con. open ();

Try
{
Cmd. executenonquery ();
}
Finally
{
Con. Close ();
}
}

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.