ASP.net Application 中使用域用户登录 
    现在做的一个程序中要求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 程序没有太大的区别,也许还有更好的方法。
 

解决方案 »

  1.   

    HttpContext.Current.User.Identity.Name
    Request.ServerVariables["LOGON_USER"].ToString()我用以上方法得到用户名时,必须是把web站点的匿名访问去掉后,然后当访问站点时出现
    登录框后,输入用户名、密码和域名成功登录后,只有这样用上面的两个方法才能得到当前
    的用户名。
    可我想实现的是不把 web站点的匿名访问去掉,使站点可以匿名访问。当用户开机时选择登录
    的域服务器(AD服务器),然后我在程序中用什么方法能得到登录的用户名。
     不知我说清楚没有。
    望知道的兄弟指点,谢谢!!
      

  2.   

    http://expert.csdn.net/Expert/topic/2907/2907002.xml?temp=.9174311