我要用AD做身份认证,如何才能实现取得当前登录的域用户名和密码?
Environment.GetEnvironmentVariable("USERDNSDOMAIN");
为什么用这句话取出来的一直是空值?救命呀!

解决方案 »

  1.   

    sample:
    http://topic.csdn.net/t/20030408/08/1631657.html
      

  2.   

    这个我确实发现有好多例子,不过我们领导要方便,他认为我已经登陆域了,就不应该再输用户名和口令,应该直接拿我这个域用户进行验证!其实我是想实现下面的功能,帮忙呀!想做一套WEB系统,不知以下功能如何实现:
    没有登录界面,首先验证已登录的用户是否为域用户,然后判断是否已登录域,最后判断该用户在域中的级别?
    请问要实现以上功能,该如何做?
    如何取得本地正在登录的用户名,如何判断是否已登录域,最后怎么判断他在域中的级别?
    请各位大虾指引我一个方向呀?最好有代码!
    我在网上已查找过,可是找到的都是要自己输入用户名的,能否自己取得本机的正在使用的登录用户名?
    我想用以下这句话取得用户名,可是取出来的一直是空值,好郁闷,哪里做错了?
    Environment.GetEnvironmentVariable("USERDNSDOMAIN");
      

  3.   

    to Knight94(愚翁):
    GZ~
    这个代表什么东东呀?您老阿会呀,教教我呀?
      

  4.   

    如何获得当前用户的密码,我真的不知道,我知道已经告诉你了。
    而且估计网上都没有类似的文章,希望你能说服你的领导,要不然你只能指望微软的support了。GZ == "关注"
      

  5.   

    to Knight94(愚翁):
    我不要用户的密码了,你有实现以下功能的CODE SAMPLE吗?
    没有登录界面,首先验证已登录的用户是否为域用户,然后判断是否已登录域,最后判断该用户在域中的级别?
      

  6.   

    Enviroment.UserName:当前登录的用户名
    Enviroment.UserDomainName:当前登录的域名至于用户在域中的级别,不太好判断。
      

  7.   

    Enviroment -> Environment
      

  8.   

    至于级别,我刚看到一个例子,不只对你是否有用
    /// <summary>
    ///  Check whether the account has admin rights or not...
    /// </summary>
    /// <returns></returns>
    public static bool AuthCheck()
    {
    AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
    WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
    WindowsIdentity identity = (WindowsIdentity)principal.Identity; return (principal.IsInRole(WindowsBuiltInRole.Administrator));
    }
      

  9.   

    to Knight94(愚翁):
    还是有问题耶!
    string userdomainname=Environment.UserDomainName;
    string username=Environment.UserName;
    取出来的userdomainname为我的机器名,不是我登录的域名,而username则是ASPNET,也不是我登录的用户名!
    我看了一下MSDN,Environment.UserName解释为获取启动当前线程的人的用户名。这个解释是否是说正在运行ASP.NET这个线程的用户名,那就可能是ASPNET。
      

  10.   

    获得用户名是aspnet,你写的是web程序,如果这样的话,就更简单了,看看下面的例子
    Getting the User Name :using three ways we can get the User Name using C#1) System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;string strName = p.Identity.Name;[ OR ]2) string strName = HttpContext.Current.User.Identity.Name.ToString();[ OR ]3) string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name    string strName = Request.ServerVariables[5]; //Finding with indexIn Above 3 Cases returnin string contains DomainName\WinNTLoggedUserName(for  Ex:  Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )Using string operations seperate the DomainName and UserName.
      

  11.   

    to Knight94(愚翁):
    十分感谢你的帮助!可是你所教的三种方法取出来的都是空值,请问是否哪里还要做设置呀?
      

  12.   

    First Step Setting  Up Web ServerTo get the windows user name through programatically. Compulsary we need to set the web server.Open the Internet Infomation Services(IIS), Select your ASP.NET Virtual Directory, right click, go to the Properties, In Properties window go to Directory Security Tab, Here You Find the "Edit" button in Anonymous access and Authentication Control. Press Edit Button. Now Authentication Methods dialogbox will appear.In this dialog box by default it is set(checked) Anonymous Access. uncheck this checkbox and go to botom of this dialog box check the Integrated windows authentication. Here why we are selecting this Default web server is logged by ISUER_MachineName so if it is window authentication web server will take the credentials from windows logged user.
      

  13.   

    to Knight94(愚翁):
    果然是高手耶!试了一下就成功了!
    嘿嘿……大虾能否指导一下在哪里能找到很多好的CODE SAMPLE呢?
    结贴!
    再次表示感谢!
      

  14.   

    simple but very useful
    http://www.syncfusion.com/FAQ/WindowsForms/others
    www.codeproject.com