2017-01-09 6 views
-1

Bin neu in Active Directory-Authentifizierung und asp.net. Ich möchte Active Directory-Authentifizierung mit folgenden Filtern implementieren: a. Der Benutzer wird sich mit seinen Systemanmeldeinformationen anmelden. b. Sie müssen den Benutzernamen und das Passwort mit AD bestätigen, wenn das Passwort/der Name nicht übereinstimmen. c. Müssen Benutzerrolle vom Server abrufen, damit ich Seitenzugriffsrechte in meiner Anwendung implementieren kann. d. Ich möchte 3 Arten von Benutzern überprüfen 1. Admin, 2. Nicht-Admin und 3. Nicht registrierte Benutzer. So kann ich basierend auf Rollen Seitenzugriffsrechte konfigurieren.Active Directory-Authentifizierung mit Benutzeranmeldeinformationen und Rollen

Antwort

0

ich fand einfache Lösung, die leicht für AD-Authentifizierung mit Benutzerrolle verwendet werden kann. Dazu benötigen wir grundsätzlich folgende Eingaben: 1. LDAPServerAddress 2. Gruppenname: admin, users oder andere Gruppe. Nachdem Sie die obigen Informationen erhalten haben, erstellen Sie ein gemeinsames Bibliotheksprojekt mit folgendem Code und importieren Sie in Ihr Projekt. Schritt 1: Erstellen Sie Bibliotheksprojekt in VS. Schritt 2: Erstellen Sie AD-Objektklasse, wie unten:

public class ActiveDirectoryInfo 
    { 
     public string UserName { get; set; } 
     public bool IsAuthentic { get; set; } 
     public string UserDisplayName { get; set; } 
     public string Password { get; set; } 
     public string LdapServerName { get; set; } 
     public string AdminGroupName { get; set; } 
     public string UserGroupName { get; set; } 
     public enum Role { Administrator, User, ReadOnly }; 
     public Role Authorization { get; set; } 
    } 

Schritt 3: Mit obiger Aufgabe und Werten überprüft AD unter Code:

public class ActiveDirectoryHelper 
{ 
    /// <summary> 
    /// Private variable for Principal Context 
    /// </summary> 
    private PrincipalContext context = null; 

    /// <summary> 
    /// Public property for Principal Context 
    /// </summary> 
    public PrincipalContext Context 
    { 
     get { return context; } 
     set { context = value; } 
    } 

    public ActiveDirectoryInfo adInfo = null; 
    /// <summary> 
    /// Constructor 
    /// </summary> 
    public ActiveDirectoryHelper(ActiveDirectoryInfo adInfo) 
    { 
     context = new PrincipalContext(ContextType.Domain, adInfo.LdapServerName + ":636", null, ContextOptions.SecureSocketLayer | ContextOptions.Negotiate); //'636 is the port used Secure connection' 
    } 

    /// <summary> 
    /// To Check if the user is Authentic in Active Directory 
    /// </summary> 
    /// <param name="userName">UserName</param> 
    /// <param name="password">Password</param> 
    /// <returns>IsAuthentic</returns> 
    public bool IsAuthenticUser(ActiveDirectoryInfo adInfo) 
    { 

     try 
     { 
      adInfo.IsAuthentic = context.ValidateCredentials(adInfo.UserName, adInfo.Password, ContextOptions.ServerBind); 
     } 

     catch (ArgumentException aex) 
     { 
      LogInfo.LogNLogUnhandledError("Invalid User Name or Password", aex); 
     } 
     catch (Exception ex) 
     { 
      LogInfo.LogNLogUnhandledError("Could not authenticate User", ex); 
     } 
     return adInfo.IsAuthentic; 
    } 

    /// <summary> 
    /// To get the Display UserName from Active Directory 
    /// </summary> 
    /// <param name="userName">UserName</param>  
    /// <returns>UserDisplayName</returns> 
    public string AuthenticUserName(ActiveDirectoryInfo adInfo) 
    { 
     try 
     { 
      UserPrincipal user = new UserPrincipal(context); 
      user.SamAccountName = adInfo.UserName; 
      // perform the search 
      PrincipalSearcher search = new PrincipalSearcher(user); 
      user = (UserPrincipal)search.FindOne(); 
      search.Dispose(); 
      adInfo.UserDisplayName = user.DisplayName; 
     } 

     catch (ArgumentException aex) 
     { 
      LogInfo.LogNLogUnhandledError("Invalid User Name ", aex.InnerException); 
     } 

     catch (Exception ex) 
     { 
      LogInfo.LogNLogUnhandledError("Error in AuthenticateUserName ", ex.InnerException); 
     } 

     return adInfo.UserDisplayName; 
    } 

    /// <summary> 
    /// To Check if the User belongs to a Authorized group in Active Directory 
    /// </summary> 
    /// <param name="userName">UserName</param> 
    /// <param name="password">Password</param> 
    /// <returns>UserRole</returns> 
    public ActiveDirectoryInfo.Role AuthorizedGroup(ActiveDirectoryInfo adInfo) 
    { 

     try 
     { 
      GroupPrincipal adminGroup = GroupPrincipal.FindByIdentity(context, IdentityType.Name, adInfo.AdminGroupName); 
      GroupPrincipal userGroup = GroupPrincipal.FindByIdentity(context, IdentityType.Name, adInfo.UserGroupName); 
      UserPrincipal user = new UserPrincipal(context); 
      user.SamAccountName = adInfo.UserName; 
      PrincipalSearcher search = new PrincipalSearcher(user); 
      user = (UserPrincipal)search.FindOne(); 
      PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups(); 

      // check if user is member of that group 
      if (groups.Contains(adminGroup)) 
      { 
       adInfo.Authorization = ActiveDirectoryInfo.Role.Administrator; 
      } 
      else if (groups.Contains(userGroup)) 
      { 
       adInfo.Authorization = ActiveDirectoryInfo.Role.User; 
      } 
      else 
      { 
       adInfo.Authorization = ActiveDirectoryInfo.Role.ReadOnly; 
      } 
     } 

     catch (System.ComponentModel.InvalidEnumArgumentException ienumarex) 
     { 
      LogInfo.LogNLogUnhandledError("Invalid Group Name", ienumarex.InnerException); 
     } 
     catch (ArgumentException aex) 
     { 
      LogInfo.LogNLogUnhandledError("Invalid User Name or Password", aex.InnerException); 
     } 

     catch (Exception ex) 
     { 
      LogInfo.LogNLogUnhandledError("User Cannot be Authorised", ex.InnerException); 
     } 

     return adInfo.Authorization; 
    } 
} 
Verwandte Themen