如何获取用户的 Active Directory 令牌?

par*_*seh 7 c# authentication client-server active-directory

我正在开发一个客户端 - 服务器应用程序。在此应用程序的使用中,客户端和服务器具有相同的 AD(Active Directory)域。

我希望我的服务器应用程序通过其 AD 用户对每个客户端进行身份验证。这意味着,当用户运行客户端应用程序的实例时,服务器应该了解哪个 AD 用户正在使用此应用程序实例并对其进行身份验证。因此,客户端应用程序必须向服务器发送一些信息。

一种解决方案是发送用户 AD 用户名。由于安全原因,此解决方案是不可接受的。

另一种解决方案是发送用户 AD 令牌(在登录 Windows 时提供给 AD 用户)。在此解决方案中,服务器可以检查此令牌的有效性,因此它可以识别客户端 AD 用户并对其进行身份验证。现在的问题是,在客户端应用程序的实现中,我不知道如何获取 AD 令牌。

我正在使用 C# 来实现客户端应用程序。你能帮我解决这个问题吗?或者您对这种身份验证有更好的解决方案吗?

Kur*_*ula 4

从 azure 门户获取 clientid/appid、密钥以及以下内容以获取令牌。单击右上角的帐户可以找到您的目录名称。

 string tenantName = "yourdirectoryName.OnMicrosoft.com";
 string authString = "https://login.microsoftonline.com/" + tenantName;
 AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
 // Config for OAuth client credentials             
 ClientCredential clientCred = new ClientCredential(clientId, appKey);
 string resource = "https://graph.windows.net";
 string token;
 try
 {
     AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
     token = authenticationResult.AccessToken;
 }
 catch (AuthenticationException ex)
 {
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("Acquiring a token failed with the following error: {0}", ex.Message);
     if (ex.InnerException != null)
     {
         Console.WriteLine("Error detail: {0}", ex.InnerException.Message);
     }
 }
Run Code Online (Sandbox Code Playgroud)

  • 这里的每个人都假设通过 Azure AD 进行身份验证,但我认为问题与 Azure AD 无关,而是与 Windows Active Directory 有关。不知道是否可以让Windows AD生成token? (2认同)