2009-03-27 2 views

Répondre

7

dans IIS, activez l'authentification intégrée de Windows, et dans le code, si vous utilisez:

Request.ServerVariables["LOGON_USER"] 

il retournera le nom d'utilisateur Windows de l'utilisateur connecté, ce MYDOMAIN \ myusername

+0

Si l'utilisateur ouvre une session anonyme sur IIS, je ne peux pas voir ses données. Cela ne fonctionne que sur l'ordinateur sur lequel IIS est en cours d'exécution. – VansFannel

+0

Ok, cela fonctionne avec la désactivation de connexion anonnale sur IIS. – VansFannel

+0

quelqu'un connaît l'équivalent java/tomcat ??? – opensas

0
System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString 
+0

Cela ne fonctionnera pas avec les applications ASP.NET, car cela renvoie les informations d'identification d'IIS, et non l'utilisateur connecté – Lennaert

+0

I J'utilise ça tous les jours, et ça marche bien pour moi, et tous les utilisateurs qui utilisent le site. – Kezzer

+1

Ensuite, vous utilisez probablement l'usurpation d'identité, ce qui est horrible et non recommandé. –

0

Vous devriez regarder dans le fournisseur d'appartenances au répertoire actif. Il est intégré à ASP.NET.

1

Voici le code C# Je l'utilise pour authentifier contre l'Active Directory

using System; 
using System.DirectoryServices; 

namespace XYZcompany.Enterprise 
{ 
    public class AuthenicationMgr 
    { 
    private static readonly int AD_ERR_LOGON_FAIL = -2147023570; 
    private static readonly string _path = "LDAP://xxx.yyy.ggg"; 
    private static readonly string _domain = "xxx.yyy.ggg"; 

    public static bool IsAuthenticated(string username, string pwd) 
    { 
     bool authenticatedFlag = true; 
     string domainAndUsername = _domain + "\\" + username; 
     DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 
     try 
     { 
     // Bind to the native AdsObject to force authentication. 
     Object obj = entry.NativeObject; 
     DirectorySearcher search = new DirectorySearcher(entry); 

     search.Filter = "(SAMAccountName=" + username + ")"; 
     search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 

     if (result == null) 
     { 
      authenticatedFlag = false; 
     } 
     else 
     { 
      authenticatedFlag = true; 
     } 
     } 
     catch (System.Runtime.InteropServices.COMException ex) 
     { 
     if (ex.ErrorCode == AD_ERR_LOGON_FAIL) 
     { 
      authenticatedFlag = false; 
     } 
     else 
     { 
      throw new ApplicationException("Unable to authenticate user due to system error.", ex); 
     } 
     } 
     return authenticatedFlag; 
    } 
    } 
} 
+0

Je ne le demande pas mais j'en aurai besoin. Je vous remercie! – VansFannel

1

Pour ASP.net, vous pouvez probablement utiliser

HttpContext.Current.User.Identity 

Si IIS est configuré correctement (pas de connexions anonymes, à moins)

Questions connexes