1. Asp.net single-point Login

Source: Internet
Author: User

1. Simple Description:

Single Sign On (SSO) is one of the most popular solutions for business integration. When developing an enterprise portal website or e-commerce system, design a unique login function for a user only on the same website to prevent a user name and password from logging on at multiple addresses.

Ii. Technical points:

Cache object for Web ApplicationsProgramFor each application, an instance of the cache object needs to be created, and the Instance remains valid as long as the corresponding application domain remains active, all information about a cache object instance must be provided through the cache attribute of the httpcontext object or the cache attribute of the page object.

III,CodeImplementation

 

 Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. Web. caching;
Using System. Data. sqlclient;

Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{

}
Protected Void Btnlogin_click ( Object Sender, eventargs E)
{
Int I = This . Checklogin (txtname. Text, txtpwd. Text );
If (I> 0 )
{
// The str_key, which is the unique identifier, must be unique. You can set rules as needed.
// For testing, the user name and password are used as the identification, and other error checks are not performed.
// Generate str_key
String Str_key = txtname. Text + " _ " + Txtpwd. text;
// Obtain the value of the given str_key in the cache.
String Str_user = convert. tostring (Cache [str_key]);
// The str_key project does not exist in the cache. The table name user is not logged on or has timed out.
If (Str_user = string. Empty)
{
// Timespan constructor is used to reload the version method and determine whether to log on.
Timespan sesstimeout = New Timespan ( 0 , 0 , Httpcontext. Current. session. Timeout, 0 , 0 );
Httpcontext. Current. cache. insert (str_key, str_key, Null , Datetime. maxvalue, sesstimeout, cacheitempriority. notremovable, Null );
Session [ " User " ] = Str_key;
// Login successful for the first time
Response. Write ( " <H2 style = 'color: red'> Hi, login successful! " );
}
Else
{
// This user's record exists in the cache. The table name has been logged on. You cannot log on again.
Response. Write ( " <H2 style = 'color: red'> sorry, you seem to have logged on! " );
Return ;
}

}
Else
{
Response. Write ( " The user name or password is incorrect !!! " );
}
}
Protected Void Btncandel_click ( Object Sender, eventargs E)
{
Txtname. Text = "" ;
Txtpwd. Text = "" ;
}
Public Int Checklogin ( String Loginname, String Loginpwd)
{
Sqlconnection con = New Sqlconnection ( " Server = (local); database = db_18; uid = sa; Pwd = " );
Sqlcommand mycommand = New Sqlcommand ( " Select count (*) from System Administrator table where user name = @ loginname and Password = @ loginpwd " , Con );
Mycommand. Parameters. Add ( New Sqlparameter ( " @ Loginname " , Sqldbtype. nvarchar, 20 ));
Mycommand. Parameters [ " @ Loginname " ]. Value = loginname;
Mycommand. Parameters. Add ( New Sqlparameter ( " @ Loginpwd " , Sqldbtype. nvarchar, 20 ));
Mycommand. Parameters [ " @ Loginpwd " ]. Value = loginpwd;
Mycommand. Connection. open ();
Int I = ( Int ) Mycommand. executescalar ();
Mycommand. Connection. Close ();
Return I;
}
}

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.