AD 能否为我的 Win 桌面应用程序提供我的 Web 服务凭据?

Can AD provide my Win desktop application with credentials for my web services?

我有一个工作的 c#/dotnet Windows 桌面应用程序,它通过在我的 Web 应用程序中访问各种 Web 服务来完成它的工作。当桌面应用程序启动时,它会提示用户输入用户名/密码,然后点击我的登录 Web 服务,其中 returns 一个会话令牌。

我有一个拥有很多用户的大型组织客户。该客户希望直接从他们的域控制器为我的组合桌面/Web 应用程序提供身份验证/授权。他们想要单点登录,所以我的桌面应用程序不会提示他们的用户输入用户名和密码。

我的桌面应用程序如何从 Windows(可能来自用户的安全主体对象)检索可用的身份验证/授权令牌?我的 Web 应用程序如何验证该令牌,以便它可以信任桌面应用程序并向其发送会话令牌?

(我的 Web 应用程序在我的环境中运行,而不是在客户的域中。)

对于纯网络应用程序客户,我使用 SAML2 和 Active Directory / 联合服务成功地做到了这一点。 SAML2 舞蹈让我用户的浏览器 POST 向客户的 AD/FS 服务器发出请求,然后 POST 将签名响应返回给我的网络应用程序。

但我不知道如何从桌面应用程序中干净地完成它。有什么智慧吗?

首先要说明的是,我从来没有这样做过,所以我不能给你确切的代码,但我可以为你指明正确的方向。

您应该能够使用 ADFS 和 Windows 集成授权 (WIA) 执行此操作。在 "pure web app" 中,浏览器在授权步骤中发送当前登录用户的凭据。在您的情况下,您的桌面应用程序需要执行浏览器通常会执行的所有操作。无论哪种方式,Web 服务端的设置应该完全相同。

HttpClient 的 C# 中,这是重要的部分:

var httpClient = new HttpClient(new HttpClientHandler() 
                  {
                      UseDefaultCredentials = true
                  });

然后,每当您的 httpClient 发送的请求受到 401 响应的质询时,它会自动重新发送带有用户 Windows 凭据的请求。这正是网络浏览器会做的事情。所以当你得到令牌时使用它。

您可能必须在请求中发送用户代理字符串,因为 ADFS 似乎 limit WIA to certain agents

获得令牌后,请在对 Web 服务的请求中使用该令牌。

关键是您要复制浏览器的功能。因此,如果您无法设置 HTTP 请求的外观,请从浏览器访问 API 中的 GET 请求,并使用浏览器的开发工具来检查流量的确切外观,并使用该信息来在您的代码中复制相同的请求。

您可以在 github(来自 jelledruyts)中查看此样本:Modern claims-based identity scenarios for .NET developers

它有使用 Azure Active Directory and/or Windows 服务器 Active Directory 联合身份验证服务的身份验证和授权示例。


我建议阅读这篇文章 Digital Identity for .NET Applications。有点旧但是很好overview/review.

Tokens come in many different formats. For today’s .NET applications, however, three kinds of tokens are most important. They are the following:

  1. User name/password token—This very simple token contains only two claims: the name of some subject and that subject’s password.
  2. Kerberos ticket—More complex than a user name/password token, a ticket includes a subject’s name, the name of the subject’s Windows domain, and other information. Kerberos tickets that are issued by Active Directory also include an extension that contains security identifiers (SIDs) that identify the subject and the groups to which this subject belongs.
  3. SAML token—The Security Assertion Markup Language (SAML) is an XML-based language that is owned by the OASIS multivendor standards group. Unlike the other token types that are described here, a SAML token doesn’t have a fixed set of claims defined for it. Instead, this kind of token can contain any claims that its creator chooses.

As soon as the claims are available, they can be used by Windows, the application, or both. The most common uses of claims include the following:

  1. Authenticating the user(...)
  2. Making an authorization decision(...)
  3. Learning about this user(...)

身份验证类型:

  1. 基于域的身份验证(例如 Kerberos 票证):

A domain-based application accepts only a single token format with a fixed set of claims. One common example is a Windows application that accepts only Kerberos tickets. This kind of application is easy to create, and it works well inside a single Windows domain. The problem is that this simplistic approach to digital identity is no longer sufficient for many applications

  1. 基于声明的身份验证(例如 SAML 令牌)

Unlike a domain-based application, a claims-based application can potentially accept multiple token formats with varying sets of claims. The token formats and claim sets that this application accepts are determined by the application itself.


身份技术

  • Active Directory (AD) 域服务(功能齐全的目录服务、Kerbero 票证的令牌源等)
  • Active Directory 联合身份验证服务 (ADFS)(支持基于声明的应用程序,SAML 令牌的令牌源
  • Windows卡片空间
  • Active Directory 轻型目录服务(AD 服务的子集)
  • Identity Life-Cycle Manager (ILM)(不同身份存储之间的同步)
  • Windows 授权管理器(RBAC 工具 - 基于角色的访问控制)
  • Active Directory 权限管理服务 (RMS)

Because AD Domain Services implements Kerberos, the default token in a Windows environment is a Kerberos ticket. To use this default, an ASP.NET application specifies Windows Integrated Authentication, while a WCF application uses an appropriate binding, such as NetTcpBinding. In either case, the following figure illustrates how a Windows application with clients in the same domain might use a Kerberos ticket and AD Domain Services


AD FS 的第一个版本仅支持 SAML 与 Web 客户端。

ADFS 1.0, supports only browser clients—a restriction that’s scheduled to change in the technology’s next release.

希望对您有所帮助。