在过去的几年里,我一直在使用这个小功能,没有任何问题来验证用户凭据.该createPrincipalContext方法返回PrincipalContextwith ContextType.Machine和机器名称.
public static bool ValidateCredentials(string username, string password, string domain = null) {
try {
using (var principalContext = createPrincipalContext(username, domain)) {
username = GetLoginInfo(username).Username;
// validate the credentials
if (principalContext.ValidateCredentials(username, password)) {
//once valid check if account is enabled
using (UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, username)) {
return user.Enabled.GetValueOrDefault(false);
}
}
}
} catch (PrincipalOperationException e) {
traceError(e);
} catch (Exception e) {
traceError(e);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我的开发机器最近自动更新到最新版本的Windows 10,从那时起,principalContext.ValidateCredentials一直抛出以下异常.
System.IO.FileNotFoundException:系统找不到指定的文件.
除了机器更新,没有其他更改.我花了最近几天在网上搜索可能导致问题的原因. …
我已经有一段时间了,我总是得到:
System.DirectoryServices.AccountManagement.PrincipalServerDownException
我认为这意味着我的连接设置(连接字符串)是错误的.
当我在Active Directory所在的计算机上的cmd上写"dsquery server"时,我得到:
"CN = DCESTAGIO,CN =服务器,CN =事先默认站点名称,CN =站点,CN =配置,DC = estagioit,DC =本地"
我通过以下方式尝试了以下连接:
1:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101", "DC=estagioit,DC=local");
Run Code Online (Sandbox Code Playgroud)
2:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/DC=estagioit,DC=local");
Run Code Online (Sandbox Code Playgroud)
3:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/CN=DCESTAGIO,DC=estagioit,DC=local");
Run Code Online (Sandbox Code Playgroud)
4:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "192.168.56.101/CN=DCESTAGIO,CN=SERVERS,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=estagioit,DC=local");
Run Code Online (Sandbox Code Playgroud)
5:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "LDAP://192.168.56.101/CN=Users,DC=estagioit,DC=local");
Run Code Online (Sandbox Code Playgroud)
还有其他一些方法......
关于什么是错的以及我如何使这种连接起作用的任何想法?
PS:ip是正确的,因为我用它来ping并且它正在工作.
PPS:我真的非常需要这个工作,如果你有任何建议他们都欢迎.
我有一个WCF服务,其中包含一个Login根据本地计算机凭据验证用户名和密码的方法,并且在看似随机的一段时间后,它将停止为某些用户工作.
实际的登录命令如下所示:
public UserModel Login(string username, string password, string ipAddress)
{
// Verify valid parameters
if (username == null || password == null)
return null;
try
{
using (var pContext = new PrincipalContext(ContextType.Machine))
{
// Authenticate against local machine
if (pContext.ValidateCredentials(username, password))
{
// Authenticate user against database
using (var context = new MyEntities(Connections.GetConnectionString()))
{
var user = (from u in context.Users
where u.LoginName.ToUpper() == username.ToUpper()
&& u.IsActive == true
&& (string.IsNullOrEmpty(u.AllowedIpAddresses)
|| u.AllowedIpAddresses.Contains(ipAddress))
select u).FirstOrDefault();
// If …Run Code Online (Sandbox Code Playgroud) 我在.NET Core 2.0中创建了一个Web应用程序,我想使用PrincipalContextfrom命名空间 System.DirectoryServices.AccountManagement.
我想再次验证用户的Active Directory,如下所示:
private static ClaimsIdentity ValidateUser(string userName, string password)
{
var domain = GetDomainByLogin(userName);
using (var pc = new PrincipalContext(ContextType.Domain, domain, null, ContextOptions.Negotiate))
{
if (!pc.ValidateCredentials(userName, password)) return null;
var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);
if (user == null)
{
throw new Exception(UserNotFound);
}
var id = new ClaimsIdentity();
id.AddClaim(new Claim(JwtClaimTypes.Subject, userName));
id.AddClaim(new Claim(JwtClaimTypes.Name, userName));
var groups = user.GetGroups();
var roles = groups.Select(x => new Claim(JwtClaimTypes.Role, x.Name));
id.AddClaims(roles);
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何使用 …
我正在使用System.DirectoryServices.AccountManagement 命名空间中的类来从我的Web应用程序与Active Directory进行交互.要针对Active Directory验证用户凭据,请使用以下代码行.
bool authSucceeded=principalContext.ValidateCredentials(userName, password);
Run Code Online (Sandbox Code Playgroud)
其中principalContext是PrincipalContext实例.当用户使用提供的凭据进行身份验证时,authSucceeded为true.但是,当下次登录策略的"更改密码"处于活动状态时,此方法将失败.对于这些用户,即使使用密码'Abcd_10'创建它们也未经过身份验证.
任何人都知道我如何在这种状态下验证用户,以便我可以将他重定向到更改密码屏幕?我已经为所有其他任务完成了代码.但只有这件事遗失了.
我只需要知道原因
principalContext.ValidateCredentials
Run Code Online (Sandbox Code Playgroud)
方法返回false,如由于密码错误/用户名无效/用户被停用或我的要求(他是否通过密码更改验证)
如有任何想法,请分享..
c# authentication active-directory windows-authentication principalcontext
我正在尝试验证用户是否在“ TestGroup”组中。该用户是“ TestGroup”组的一部分,即使我正在获取retval = false @line(retVal = user.IsMemberOf(groupPrincipal);),并且在事件查看器中,它也将msg显示为“用户名或密码不正确” ”。
你能帮我吗
string userName = this.Request.ServerVariables["AUTH_USER"];
if (ValidateUser(userName) == false)
Response.Redirect("Error.aspx?errormsg=" + userName + " does not have permission to view this page");
public static bool ValidateUser(String userName)
{
bool useGroupAuthorization = true;
if (useGroupAuthorization)
return GroupLookup(userName, "TestGroup");
}
private static bool GroupLookup(string userName, string groupName)
{
System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
appLog.Source = "Test App";
bool retVal = false;
PrincipalContext pc = null;
UserPrincipal user = null;
GroupPrincipal groupPrincipal = null; …Run Code Online (Sandbox Code Playgroud) using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Domain, UserName, Password))
{
UserPrincipal U = new UserPrincipal(ctx);
U.GivenName = strFirstName;
U.Surname = strLastName;
U.EmailAddress = strEmail;
PrincipalSearcher srch = new PrincipalSearcher(U);
foreach (var principal in srch.FindAll())
{
var p = (UserPrincipal)principal;
if (!User.Any(x => x.Email == p.EmailAddress))
{
MyUserDataset.UserRow User = User.NewUserRow();
User.FirstName = p.GivenName;
User.LastName = p.Surname;
User.UserName = p.SamAccountName;
User.Email = p.EmailAddress;
User.AddUserRow(User);
}
}
User.AcceptChanges();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的PrincipalContext类来建立与目标目录的连接,并指定用于对目录执行操作的凭据.
有没有人知道如何在PrincipalContext构造函数中指定连接超时?我遇到连接超时问题我想知道我是否可以控制连接超时多长时间.
在我的开发机器上,我不得不安装AD-LDS.原则上它工作正常,但是第一次通过PrincipalContext类连接到AD-LDS非常慢(30秒+).在我看来,它首先尝试连接到一些不存在的主机或目录,然后在超时(30秒)后连接到我的AD-LDS并执行它应该做的事情.
与LDP.exe和SSL连接时观察到的相同行为.但是使用ADSI-Edit,通过SSL连接速度非常快.所以通过非SSL连接.
我看着是否能在小提琴手中看到一些东西,但什么都没有.同样在事件日志中我什么都找不到.也许它与证书查找有关?它是用makecert自签名的.
更新
在此期间,我观察到一个可能提示的小事:在系统事件日志中,第一次建立与AD-LDS的SSL连接时,将显示以下消息:
machineName在没有配置的DNS服务器响应后,名称_ldap._tcp.[ ]的名称解析超时
但是,该消息仅注册一次,但每次连接到服务器需要30secs +.我还尝试在hosts文件中输入相应的条目,但没有任何改变.
附加信息
可能这不是证书的问题,但可能有助于解决问题.因此,我在这里创建证书的方式(或多或少的货物代码):
RootAuthority
makecert -pe -n "CN=MyDevRootAuthority" -ss my -sr LocalMachine -a sha1 -sky signature -r "MyDevRootAuthority.cer"
Run Code Online (Sandbox Code Playgroud)
服务器证书
makecert -pe -n "CN=[MyComputerName]" -ss my -sr LocalMachine -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1 -in "MyDevRootAuthority" -is MY -ir LocalMachine -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 "MyTestCertificate.cer"
Run Code Online (Sandbox Code Playgroud)
创建后,我将根权限移动到受信任的权限,并授予所需的权限.
我正在使用LdapAuthentication将用户登录到Active Directory.我想找到用户所属的所有组.我使用以下代码:
string adPath = "LDAP://OU=HR Controlled Users,OU=All Users,DC=myDomain,DC=local";
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if (true == adAuth.IsAuthenticated("myDomain", txtLoginEmail.Text, txtLoginPassword.Text))
{
string email = txtLoginEmail.Text;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.Name, email);
foreach (var group in user.GetGroups())
{
Console.WriteLine(group.Name);
}
}
}
}
catch(Exception e) { /* Handle Error */ }
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我调用UserPrincipal.FindByIdentity()时,我总是得到一个空值,即使用户身份验证按预期工作.
为什么会这样?代码或我的方法有问题吗?这是在ASP.NET 4.0 WebForms应用程序中运行.
更新:
显然我一直在使用错误的IdentityType(cn).我检查了调试,帐户名称是"UserA".
所以我尝试手动使用以下代码:
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.Name, "UserA");
Run Code Online (Sandbox Code Playgroud)
但我仍然无效.
更新2(已解决):
这个问题有两个问题.在声明时我需要指定域控制器的名称PrincipalContext.
using (PrincipalContext context …Run Code Online (Sandbox Code Playgroud) 我也有使用插件和appdomains的长期服务,并且因使用目录服务而导致内存泄漏.请注意,我使用的是system.directoryservices.accountmanagement,但我理解它使用相同的底层ADSI API,因此容易出现相同的内存泄漏.
我查看了所有CLR内存计数器,并且内存没有泄漏,并且都是在强制GC或卸载appdomain时返回的.泄漏是私有字节,不断增长.我在这里搜索并看到了一些与使用ADSI API时内存泄漏相关的问题,但它们似乎表明只需遍历directorysearcher即可解决问题.但正如您在下面的代码中看到的那样,我在foreach块中执行此操作,但内存仍然被泄露.有什么建议?这是我的方法:
public override void JustGronkIT()
{
using (log4net.ThreadContext.Stacks["NDC"].Push(GetMyMethodName()))
{
Log.Info("Inside " + GetMyMethodName() + " Method.");
System.Configuration.AppSettingsReader reader = new System.Configuration.AppSettingsReader();
//PrincipalContext AD = null;
using (PrincipalContext AD = new PrincipalContext(ContextType.Domain, (string)reader.GetValue("Domain", typeof(string))))
{
UserPrincipal u = new UserPrincipal(AD);
u.Enabled = true;
//u.Surname = "ju*";
using (PrincipalSearcher ps = new PrincipalSearcher(u))
{
myADUsers = new ADDataSet();
myADUsers.ADUsers.MinimumCapacity = 60000;
myADUsers.ADUsers.CaseSensitive = false;
foreach (UserPrincipal result in ps.FindAll())
{
myADUsers.ADUsers.AddADUsersRow(result.SamAccountName, result.GivenName, result.MiddleName, result.Surname, result.EmailAddress, result.VoiceTelephoneNumber,
result.UserPrincipalName, result.DistinguishedName, result.Description); …Run Code Online (Sandbox Code Playgroud) 我已经安装了openldap for windows服务器,并使用LDAPAdmin其默认值连接至该服务器:
Server=ldap://localhost:389
Base:dc=maxcrc,dc=com
UserName:cn=Manager,dc=maxcrc,dc=com
Password:secret
Run Code Online (Sandbox Code Playgroud)
现在,我想用来PrincipalContext向用户添加用户ou=People
。问题是,我什至无法使用PrincipalContext连接到服务器。我已经在网上搜索了很多关于如何连接到ldap服务器的答案,但是没有一个对我有用。我一直在为PrincipalContext构造函数提供一个NullReferenceException或多个ServerNotFoundException不同的组合参数。我以为可以使用PrincipalContext作为以下之一:
new PirncipalContext(ContextType.Domain,"maxcrc.com","dc=maxcrc,dc=com")
Run Code Online (Sandbox Code Playgroud)
要么
new PrincipalContext(ContextType.ApplicationDirectory,"localhost:389","dc=maxcrc,dc=com")
Run Code Online (Sandbox Code Playgroud)
但是它们都没有起作用。有人说我应该提供用户名和密码,所以我做到了,但我不断出现异常。
因此,请告诉我如何使用PrincipalContext连接到openldap?
PS我的计算机已加入Active Directory域控制器。
我有一个使用 SSL 的 PrincipalContext。这在使用 Context.ValidateCredentials() 之类的方法时效果很好。但是当我需要使用 UserPrincipal.FindByIdentity() 查找用户时,我收到以下错误:
System.Runtime.InteropServices.COMException:服务器不愿意处理请求。在 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 在 System.DirectoryServices.DirectoryEntry.Bind() 在 System.DirectoryServices.DirectoryEntry.get_SchemaEntry() 在 System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de) 在 System.DirectoryServices .AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options) at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry) at System.DirectoryServices.AccountManagement.PrincipalContext()-DoLDAPDirectory -- 内部异常堆栈跟踪结束 --- 在 System.DirectoryServices。
我的方法:
public List<string> GetUserInfo(string user) {
var list = new List<string>();
using (var context = new PrincipalContext(ContextType.Domain, "xxxx.xxxx.xxxx:636", "DC=xxxx,DC=xxxx,DC=xxxx", ContextOptions.SimpleBind | ContextOptions.Sealing | ContextOptions.SecureSocketLayer)) {
var uP = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user);
//Do stuff with uP
return list;
}
Run Code Online (Sandbox Code Playgroud)
但这工作正常:
public …Run Code Online (Sandbox Code Playgroud) principalcontext ×12
c# ×10
asp.net ×2
.net ×1
adam ×1
adlds ×1
adsi ×1
asp.net-core ×1
c#-4.0 ×1
iprincipal ×1
ldap ×1
openldap ×1
ssl ×1
wcf ×1