在asp。net中获取域服务器上的登录名

brlyy 2009-11-09 06:04:50
我现在用。net做一个登录,输入登录名跟密码和域服务器上的名字和密码比较,如果匹配就登录成功!

现在是我要怎么读取域服务器的登录名, 各位高手帮帮忙呀!!!!!
最好有实例,谢谢啦!!
...全文
620 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovelizheng 2010-03-29
  • 打赏
  • 举报
回复
使用ldap连接到域服务器上,然后查找属性为"samaccountname=你的登录名,pwd=你的密码"的用户即可,
brlyy 2009-11-10
  • 打赏
  • 举报
回复
这样不行呀,我要在后面读取域服务器上的组织架构的,所以还是要读取。
还想懒够 2009-11-10
  • 打赏
  • 举报
回复
直接将IIS设为Windows集成认证,而不使用匿名用户访问,那啥验证代码都不用写了
brlyy 2009-11-10
  • 打赏
  • 举报
回复
还是不懂呀,郁闷死了,
yan267 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 brlyy 的回复:]
C# code
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.Runtime.InteropServices;  //必要引用
using System.Security.Principal;    //必要引用
/**//// <summary>
/// UserLoginForDomain 的摘要说明
/// 适用ASP.NET 2.0
/// Windows XP 调试成功
/// 调用”advapi32.dll“win32 API
/// </summary>

namespace  UserLoginForDomain
{
    public class UserLoginForDomainDAO
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        WindowsImpersonationContext impersonationContext;

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern int LogonUser(String lpszUserName,
                                          String lpszDomain,
                                          String lpszPassword,
                                          int dwLogonType,
                                          int dwLogonProvider,
                                          ref IntPtr phToken);
        [DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        public extern static int DuplicateToken(IntPtr hToken,
                                          int impersonationLevel,
                                          ref IntPtr hNewToken);
        /**/
        /// <summary>
        /// 输入用户名、密码、登录域判断是否成功
        /// </summary>
        /// <example>
        /// if (impersonateValidUser(UserName, Domain, Password)){}
        /// </example>
        /// <param name="userName">账户名称,如:string UserName = UserNameTextBox.Text; </param>
     /// <param name="domain">要登录的域,如:string Domain  = DomainTextBox.Text; </param>        /// <param name="password">账户密码, 如:string Password = PasswordTextBox.Text; </param>
        /// <returns>成功返回true,否则返回false </returns>
        public bool impersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public void undoImpersonation()
        {
            impersonationContext.Undo();
        }
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

   
}


上面代码中的 要登录中的域  是指什么??? 是域服务器的ip还是什么??
[/Quote]
域名
brlyy 2009-11-10
  • 打赏
  • 举报
回复
各位帮帮忙呀,这个问题已经困扰我很久了,
brlyy 2009-11-10
  • 打赏
  • 举报
回复
C# code
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.Runtime.InteropServices; //必要引用
using System.Security.Principal; //必要引用
/**//// <summary>
/// UserLoginForDomain 的摘要说明
/// 适用ASP.NET 2.0
/// Windows XP 调试成功
/// 调用”advapi32.dll“win32 API
/// </summary>

namespace UserLoginForDomain
{
public class UserLoginForDomainDAO
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
/**/
/// <summary>
/// 输入用户名、密码、登录域判断是否成功
/// </summary>
/// <example>
/// if (impersonateValidUser(UserName, Domain, Password)){}
/// </example>
/// <param name="userName">账户名称,如:string UserName = UserNameTextBox.Text;</param>
/// <param name="domain">要登录的域,如:string Domain = DomainTextBox.Text;</param> /// <param name="password">账户密码, 如:string Password = PasswordTextBox.Text;</param>
/// <returns>成功返回true,否则返回false</returns>
public bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
return true;
else
return false;
}
else
return false;
}
else
return false;
}

public void undoImpersonation()
{
impersonationContext.Undo();
}
//
// TODO: 在此处添加构造函数逻辑
//
}


}


上面代码中的 要登录中的域 是指什么??? 是域服务器的ip还是什么??
yan267 2009-11-10
  • 打赏
  • 举报
回复
或者IIS站点中去掉匿名登录,勾选Windows集成验证就可以了。不过就是会弹出对话框,如果想用本身页面的话,用上面那个类吧
yan267 2009-11-10
  • 打赏
  • 举报
回复

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.Runtime.InteropServices; //必要引用
using System.Security.Principal; //必要引用
/**//// <summary>
/// UserLoginForDomain 的摘要说明
/// 适用ASP.NET 2.0
/// Windows XP 调试成功
/// 调用”advapi32.dll“win32 API
/// </summary>

namespace UserLoginForDomain
{
public class UserLoginForDomainDAO
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public extern static int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
/**/
/// <summary>
/// 输入用户名、密码、登录域判断是否成功
/// </summary>
/// <example>
/// if (impersonateValidUser(UserName, Domain, Password)){}
/// </example>
/// <param name="userName">账户名称,如:string UserName = UserNameTextBox.Text;</param>
/// <param name="domain">要登录的域,如:string Domain = DomainTextBox.Text;</param>
/// <param name="password">账户密码, 如:string Password = PasswordTextBox.Text;</param>
/// <returns>成功返回true,否则返回false</returns>
public bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
return true;
else
return false;
}
else
return false;
}
else
return false;
}

public void undoImpersonation()
{
impersonationContext.Undo();
}
//
// TODO: 在此处添加构造函数逻辑
//
}


}



brlyy 2009-11-10
  • 打赏
  • 举报
回复
不明白,能说的具体点吗??怎么配置呀??谢谢!!!
PSSonyXbox 2009-11-10
  • 打赏
  • 举报
回复
在web.config里配置不就可以吗
不需要你比较,登录验证是一样的
或 1楼的答案
brlyy 2009-11-10
  • 打赏
  • 举报
回复
这个也是获得自己电脑上的域名呀
wen1512 2009-11-10
  • 打赏
  • 举报
回复
Request.ServerVariables.Get("LOGON_USER").ToString();
brlyy 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lzsh0622 的回复:]
C# codeUser.Identity.Name;//读取域登录用户名,格式为Domainname\username (域名名称\用户名称)//读取服务器所在域全称:IPHostEntry myHost=new IPHostEntry();
myHost= Dns.GetHostByName(Dns.GetHostName());this.labHost.Text= myHost.HostNam?-
[/Quote]

我试过了,读出来的是我自己机子上所设定的域名,不是域服务器上的域名呀,我要实现的是在界面输入用户名和密码然后去和域服务器中的用户名和密码比较!!能否举个详细的实例呀!!谢谢!!!
brlyy 2009-11-10
  • 打赏
  • 举报
回复
有没有人能帮帮我呀、、、、、、、、、、、、、、、、、、、
brlyy 2009-11-10
  • 打赏
  • 举报
回复
这个是本机上的,但是我要怎么跟域服务器上所设置的账户名去匹配呢???
mzjmicrosoft 2009-11-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lzsh0622 的回复:]
C# codeUser.Identity.Name;//读取域登录用户名,格式为Domainname\username (域名名称\用户名称)//读取服务器所在域全称:IPHostEntry myHost=new IPHostEntry();
myHost= Dns.GetHostByName(Dns.GetHostName());this.labHost.Text= myHost.HostNam?-
[/Quote]

这不行嘛
brlyy 2009-11-10
  • 打赏
  • 举报
回复
各位大哥大姐,有没有具体的实际例子,给我参考参考呀,感激不尽。。。。
nosuchtracter 2009-11-09
  • 打赏
  • 举报
回复
这个俺不会
是否和form验证差不多呢?
FormsIdentity
lzsh0622 2009-11-09
  • 打赏
  • 举报
回复
User.Identity.Name;  //读取域登录用户名,格式为Domainname\username (域名名称\用户名称)

//读取服务器所在域全称:
IPHostEntry myHost = new IPHostEntry();
myHost = Dns.GetHostByName(Dns.GetHostName());
this.labHost.Text = myHost.HostName.ToString();

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧