c# - 自动登录 Active Directory

标签 c# .net active-directory single-sign-on directoryservices

我在桌面 Active Directory 应用程序中为用户自动登录时遇到一些困难。我可能正在尝试进行 SSO,但我的印象是它仅适用于 Web 应用程序。

我的代码是这样的:

PrincipalContext theContext = new PrincipalContext(ContextType.Domain);
if (theContext.ValidateCredentials(null, null))
    Console.WriteLine("Credentials have been validated. Tell your friends.");
else
    Console.WriteLine("Invalid credentials");
UserPrincipal user = new UserPrincipal(theContext, "uatu", "Passw0rd", true);
user.Save();

正在创建PrincipalContext,没有错误,并且我正在验证凭据。我认为这将验证我作为登录到位于 Active Directory 域下的计算机的用户的身份。我可以找到用户和组。但是当我调用 user.Save() 时,我收到错误“访问被拒绝”。我实际上是以访客用户身份进入 Active Directory 的吗?

如果我在 ValidateCredentials 中设置用户名和密码,则没有帮助。

PrincipalContext theContext = new PrincipalContext(ContextType.Domain);
if (theContext.ValidateCredentials("<username>", "<password", ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing))
    Console.WriteLine("Credentials have been validated. Tell your friends.");
else
    Console.WriteLine("Invalid credentials");
UserPrincipal user = new UserPrincipal(theContext, "uatu", "Passw0rd", true);
user.Save();

该代码在 user.Save() 上仍然失败。 如果我在 PrimaryContext 构造函数中显式设置用户名和密码以匹配自己作为登录用户的身份,那么我就会成功。

PrinicipalContext  theContext = new PrincipalContext(ContextType.Domain,"<address>", "<domain context>", "<username>", "<password>");
UserPrincipal user = new UserPrincipal(theContext, "uatu", "Passw0rd", true);
user.Save();

该代码成功。但我不希望用户在使用完全相同的凭据登录计算机后登录我的应用程序。

我听说过一些有关“联属应用程序”的内容,因此我想知道是否必须让 Active Directory 知道它可以信任我的应用程序。我对细节仍然很模糊,不知道这是否是错误的方向。

有人知道我应该做什么吗?

最佳答案

如果您尝试修改UserPrincipals ,您有几个选择:

  1. 用户已通过 Windows 身份验证,成为有权编辑 Active Directory 的用户:
    • 使用 PrincipalContext 的构造函数不需要用户名/密码
      • 这将以当前登录用户的身份运行上下文
    • 运行查询,var usr = UserPrincipal.FindByIdentity(ctx, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9dfff2ffddf9f2f0fcf4f3b3f1f2fefcf1" rel="noreferrer noopener nofollow">[email protected]</a>");
    • usr 进行操作对象
    • 调用 usr.Save();查询返回的用户。
  2. 用户已通过 Windows 身份验证,但您必须“模拟”具有 AD 权限的用户
    • 使用 PrincipalContext 的构造函数它需要用户名/密码
      • 这会将上下文作为您传入的凭据运行
    • 运行查询,var usr = UserPrincipal.FindByIdentity(ctx, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2808d80a2868d8f838b8ccc8e8d81838e" rel="noreferrer noopener nofollow">[email protected]</a>");
    • usr 进行操作对象
    • 调用 usr.Save();查询返回的用户。

根据您上面的解释,我假设您需要选项#1。 ValidateCredentials();仅用于验证凭据,如果您提供的凭据有效,它将返回 true/false。调用它没有持久的影响,它只是验证。如果需要模拟用户,则需要使用PrincipalContext需要凭据的构造函数。

关于c# - 自动登录 Active Directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866007/

相关文章:

.net-3.5 - Active Directory中NativeGuid和Guid之间的区别

c# - 在 IIS 中设置应用程序池的优先级

c# - 创建动态公式

c# - 对象关系映射

c# - JwtSecurityToken 不会在应该过期的时候过期

active-directory - Active Directory 中 'country' 字段的值是什么?

java - 密码重置使用 UnboundID 执行目录策略

c# - 此代码段中是否存在无法访问的代码?我不这么认为,但 Resharper 告诉我的不是这样

c# - 在 .NET 4.0 中使用 Web 服务时出现连接错误

.net - .NET 中有处理 Modbus 协议(protocol)的好库吗?