首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 使用域帐户登录解决方法

2012-02-17 
C# 使用域帐户登录想为部门做个小程序,不过又不想做用户管理部分,恰好所有的用户都在域上。例如 sample.com

C# 使用域帐户登录
想为部门做个小程序,不过又不想做用户管理部分,恰好所有的用户都在域上。例如 sample.com。我想先做个login,输入用户自己的域帐户和密码后,login 可以检查该用户在域上的账户和密码是否存在和正确,然后返回个bool就好,请问该怎么做?简单点的就OK,谢谢各位

[解决办法]
把网站内的一个目录,目录下放一个文件,文件内容可以为空,在配iis的时候,把这个目录设置成需要Windows身份验证,登录时用webrequest请求一下目录下的文件,如果GetResponse() 不报错,则验证通过,否则就是没有该用户,我们现在就是这么做的
[解决办法]

C# code
using System;using System.DirectoryServices;namespace ConsoleApplication1{    internal class Program    {        private static void Main(string[] args)        {            string name = "jim green"; // RDN format            string logonName = @"xxx\jgreen"; //pre windows 2000 format            Console.WriteLine(IsUserValid(name, "the-password"));            Console.WriteLine(IsUserValid(logonName, "the-password"));            Console.ReadLine();        }        public static bool IsUserValid(string name, string pwd)        {            DirectoryEntry rootEntry = new DirectoryEntry("LDAP://xxx.com", name.ToLower(), pwd,AuthenticationTypes.ServerBind);            try            {                // Bind to the native object to force authentication                Object nativeObject = rootEntry.NativeObject;                return true;            }            catch (Exception ex)            {                Console.WriteLine(ex);                return false;            }        }    }} 

热点排行