c#-3.0 – 使用默认凭据以调用DirectoryEntry

我在登录页面工作,逻辑就像 – >

try 
{
    DirectoryEntry LDAPLogin = new DirectoryEntry(ConfigurationSettings.AppSettings ["LDAPPath"].ToString(), Usuario, Txt_Contrasenia.Text.ToString());

    if (LDAPLogin.NativeGuid != LDAPLogin.Name)
       ValidarGrupo();
}
catch (Exception exc)
{
   Label_Info.Text = "Sus credenciales no son validas: " + Usuario.ToString() + " " + exc.Message;
}

如果用户输入权限凭证,我调用一个方法ValidarGrupo,该方法在AD中为一组用户实现查找

我想用UseDefaultCredentials替换用户名和密码,以避免用户输入用户名和密码,登录页面使用在计算机上登录的用户的凭据.

最佳答案

I would like to replace the username
and password with
UseDefaultCredentials in order to
avoid that the user has to enter the
username and password and the Login
pages use the credentials of the user
that is login on the machine.

所以你基本上想检查当前登录的用户是否有效?

我相信您只需创建DirectoryEntry而无需指定任何用户名/密码 – 在这种情况下,System.DirectoryServices将自动使用当前用户凭据:

string ldapPath = ConfigurationSettings.AppSettings ["LDAPPath"];
DirectoryEntry LDAPLogin = new DirectoryEntry(ldapPath);

我相信这应该足够了!

点赞