从Azure Active Directory获取toke的意外结果

时间:2016-04-05 13:10:31

标签: c# azure active-directory

我正在开发客户端项目,我需要从Azure AD获取用户并需要存储在应用程序数据库中。为此,我添加了页面以获取设置和按钮来测试设置详细信息。我使用下面的代码来获取访问令牌

  string authString = authnEndpoint + tenant;
  AuthenticationContext authenticationContext = new AuthenticationContext(authString);
  ClientCredential clientCred = new ClientCredential(clientId, clientSecret);  
  AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
  authenticationResult.AccessToken

在以下情况下工作正常

当输入错误的细节时,我在AcquireToken方法上获得异常,而当我给出正确的详细信息时,它会给我访问令牌

在以下情况下不起作用

它的工作方式不正常,当输入正确的详细信息时,我正在获取访问令牌,之后我输入错误的详细信息,现在它再次给我访问令牌。我只有在重新启动应用程序时才能解决这个问题

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您正在使用默认情况下启用了令牌缓存的构造函数AuthenticationContext(String)。因此,即使您的输入在某段时间内输入不正确,它也会为您提供令牌。这是一个解决方案。

AuthenticationContext authenticationContext = new AuthenticationContext(authString, null);

使用构造函数AuthenticationContext(String, TokenCache)代替。将TokenCache设置为null将禁用令牌缓存。