2015-04-01 117 views
2

我正在开发一个客户端 - 服务器应用程序。在使用此应用程序时,客户端和服务器具有相同的AD(Active Directory)域。如何获取用户的Active Directory令牌?

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

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

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

我正在使用C#实现客户端应用程序。你能帮我解决吗?或者你有更好的解决方案来进行这种认证吗?

回答

1

从azure门户获取clientid/appid,secretkey并获取令牌。 您的目录名称可以通过将您的帐户固定在右上角找到。

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); 
       } 
      } 
+0

嗨@Kurkula,你能为我解释什么应该是“tenantName”,“clientId”,“appKey”和“资源”的值,我在哪里可以得到它们?谢谢。我对上面的作者有同样的问题。 – Anthony 2018-02-27 16:59:57

相关问题