[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
[DllImport("Kernel32.dll")]
public static extern int GetLastError();
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
private void LogON()
{
IntPtr tokenHandle = new IntPtr(0);
IntPtr dupeTokenHandle = new IntPtr(0);
try
{
string UserName, MachineName ,PassWord;
// Get the user token for the specified user, machine, and password using the
// unmanaged LogonUser method.
MachineName = this.txtMachinename.Text;
UserName = this.txtUserName.Text;
PassWord = this.txtpass.Text;
// const int LOGON32_PROVIDER_WINNT50 = 2;
// //This parameter causes LogonUser to create a primary token.
// const int LOGON32_LOGON_INTERACTIVE = 3;
const int SecurityImpersonation = 2;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(UserName, MachineName, PassWord,
Convert.ToInt32(this.txtdwLogonType.Text),Convert.ToInt32(this.txtdwLogonProvider.Text)
,ref tokenHandle);
// Check the identity.
bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);
if (false == retVal)
{
return;
}
else
{
this.TextBox1.Text = "OK";
}
// The token that is passed to the following constructor must
// be a primary token in order to use it for impersonation.
WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();
// Check the identity.
// Stop impersonating the user.
impersonatedUser.Undo();
// Check the identity.
// Free the tokens.
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if (dupeTokenHandle != IntPtr.Zero)
CloseHandle(dupeTokenHandle);
How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q316748
不知道这个咋的,看看ing……
ASP.NET 必须需要匿名登录,如果没有则会跳出帐号密码框,先通过IIS认证,才能进入ASP.NET的认证,我们讨论的身份验证都是ASP.NET的。
具体认证过程可参考MSDN帮助上的:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconaspnetdataflow.htm