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
-1
A
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
- 1. Azure Active Directory-Rollen
- 2. Azure Active Directory-Gruppen/-Rollen
- 3. mit Windows-Authentifizierung mit Active Directory-Gruppen als Rollen
- 4. Erweitern der Active Directory-Sicherheit durch Aktionen und Rollen
- 5. WCF + Benutzeranmeldeinformationen
- 6. JMS mit verpflichtender Skalierbarkeit (Active-Active -...- Active) und Bestellung?
- 7. Rollen mit Meteor und Reagieren
- 8. AWS Cognito Facebook und Google Plus Benutzeranmeldeinformationen
- 9. Azure Android SDK - Active Directory-Authentifizierung - Wie bekomme ich Rollen?
- 10. ASP Core Azure Active Directory Anmeldung Rollen verwenden
- 11. Listen und ändern Benutzeranmeldeinformationen in Svn
- 12. Wie binden und Abfrage LDAP-Server ohne Benutzeranmeldeinformationen zu spezifizieren
- 13. credentialcache.defaultcredentials falsche Benutzeranmeldeinformationen zurückgegeben
- 14. IdentityServer, Ansprüche und Rollen
- 15. iTunesConnect Benutzer und Rollen
- 16. Wie ordne ich Netzwerklaufwerk mit Benutzeranmeldeinformationen zu
- 17. .NET-Rollen und IIS7
- 18. ASP.NET - Mitgliedschaft und Rollen
- 19. Magento: Rollen und Benutzer
- 20. Wie kann ich ASP.NET Forms Authentication UX bereitstellen, während Active Directory-Rollen- und Authentifizierungsanbieter verwendet werden?
- 21. ASP .NET-Authentifizierung gegen Active Directory und Rollen über ASP.NET-Rollenanbieter
- 22. Rollen Autorisierung mit Rails 3 und Devise
- 23. Überprüfen von Rollen und Authentifizierung mit Passport.js
- 24. Admin Rollen mit Loopback und AngularJS
- 25. Erhalten Sie Benutzeranmeldeinformationen von Sharepoint Active Directory, dann verwenden Sie es in ASP.Net
- 26. Rollen zählt mit TSQL
- 27. Hibernate Benutzermodell Benutzer-Rollen und Rollen nicht sparend
- 28. Authentifizieren Rollen und Anmeldeseite für verschiedene Rollen spielen
- 29. ASP.NET rollenbasierte Sicherheit mit AzMan und AD Rollen
- 30. Beispiele mit Active Directory/LDAP-Gruppen für Berechtigungen \ Rollen in Rails App