Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
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. runtime. interopservices;
Using system. Security. Principal;
Namespace yutest
{
Public partial class _ default: system. Web. UI. Page
{
[Dllimport ("advapi32.dll", charset = charset. Auto)]
Public static extern bool logonuser (string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, out int phtoken );
Protected void page_load (Object sender, eventargs E)
{
String AAA = system. Threading. thread. currentprincipal. Identity. Name;
// String BBB = system. Threading. thread. currentprincipal. Identity. N;
// System. environment. userdomainname
// System. environment. Username
}
Protected void button#click (Object sender, system. eventargs E)
{
// Verify that user input is empty
If (tdomain. Text. Trim (). length> 0 & tusername. Text. Trim (). length> 0 & tpassword. Text. Trim (). length> 0)
{// Call the function login (string username, string password, string domain)
// Implement Windows login
If (LOGIN (tusername. Text. Trim (), tpassword. Text. Trim (), tdomain. Text. Trim () = true)
{// Display logon success Information
Loginmsg. Text = "Logon successful !!! ";
Loginmsg. Visible = true;
Return;
}
Else
{// Display logon Failure Information
Loginmsg. Text = "Logon Failed. Please enter the user name, password, and system domain name again !!! ";
Loginmsg. Visible = true;
}
}
}
Private bool login (string username, string password, string domain)
{// Obtain the user name and System domain name
String text1 = domain. Trim ();
String text2 = username. Trim ();
Text2 = text2.replace ("/", @ "\"); // process the symbol "/"
Int num1 = text2.indexof ('\'); // obtain the index of the symbol "\"
If (num1! =-1)
{// Format the user name and System domain name
Text1 = text2.substring (0, num1 );
Text2 = text2.substring (num1 + 1 );
}
Else
{// Format the user name and System domain name
Num1 = text2.indexof ('@');
If (num1! =-1)
{
Text1 = text2.substring (num1 + 1 );
Text2 = text2.substring (0, num1 );
}
}
// Call the authenticateuser () function to log on to Windows.
Return authenticateuser (text2, password. Trim (), text1 );
}
Private bool authenticateuser (string username, string password, string domain)
{// Set the user logon success flag
Bool flag1 = false;
Try
{
Int num1; intptr ptr1;
// Call the Windows logon API
If (! Logonuser (username, domain, password, 2, 0, out num1 ))
{// Return the logon result
Return flag1;
}
// Call Windows logon in. net
Ptr1 = new intptr (num1 );
Windowsidentity identity1 = new windowsidentity (ptr1 );
Windowsprincipal principal1 = new windowsprincipal (identity1 );
Httpcontext. Current. User = principal1;
// Set the system cookie and redirect page
Formsauthentication. setauthcookie (principal1.identity. Name, false );
Formsauthentication. redirectfromloginpage (username, false );
Flag1 = true;
}
Catch (exception ){}
Return flag1;
}
}
}