轉自:http://weblogs.3322.org/
現在做的一個程式中要求ASP.net 程式可以使用已經存在的域使用者來登入(而且為了與其它程式介面一致一定要使用 Forms 登入),尋找了一些相關的資料發現還是可以實現的。
主要還是依靠 advapi32.dll 中的 LogonUser API 函數。
using System.Web.Security;
using System.Runtime.InteropServices;
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
void Login_Click(Object sender, EventArgs E)
{
IntPtr token = IntPtr.Zero;
if(LogonUser(UserName.Value,
UserDomain.Value,
UserPass.Value,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref token) != 0)
{
FormsAuthentication.RedirectFromLoginPage(UserName.Value,
PersistCookie.Checked);
}
else
{
lblResults.Text = "Invalid Credentials: Please try again";
}
}
其它方面的使用與普通的forms 程式沒有太大的區別,也許還有更好的方法。
附註:技術的連貫性體現