ADFS:以编程方式获取令牌

问题描述 投票:0回答:1

因此,我们构建了一组由ADFS(MSAL)保护的Azure功能

我们在ADFS中配置了一个应用程序,并使我们的Android客户端运行良好。

我们现在想要进行一些API测试,因此我们希望以编程方式生成Auth令牌来测试API

我无法获得以下代码,也许我的租户ID错误,在App配置中,它是一个GUID(42b03d0b-d7f2-403e-b764-0dbdcf0505f6),但是示例说它是我们的域名

string userName = "-";
string password = "-";
string clientId = "ee13c922-bf4b-4f0a-ba39-ea74e1203c6e";
var credentials = new UserPasswordCredential(userName, password);
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/acostaonline.onmicrosoft.com");
var result = await authenticationContext.AcquireTokenAsync("https://graph.windows.net", clientId, credentials);

UPDATE

因此将代码更改为MSAL并仍尝试通过用户名和密码登录。现在它只是超时了

string authority = "https://login.microsoftonline.com/42b03d0b-d7f2-403e-b764-0dbdcf0505f6/";
string[] scopes = new string[] { "user.read" };
PublicClientApplication app = new PublicClientApplication("ee13c922-bf4b-4f0a-ba39-ea74e1203c6e", authority);
var accounts = await app.GetAccountsAsync();

Microsoft.Identity.Client.AuthenticationResult result = null;
      if (accounts.Any())
      {
         result = await app.AcquireTokenSilentAsync(scopes, accounts.FirstOrDefault());
      }
      else
      {
          try
          {
             var securePassword = new SecureString();
             foreach (char c in "PASSWORD")        // you should fetch the password keystroke
                securePassword.AppendChar(c);  // by keystroke

             result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "[email protected]",
                                                                           securePassword);
          }
     }

错误

SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应172.26.200.77:443

azure adfs msal
1个回答
0
投票

您提供的代码似乎是使用ADAL而不是MSAL。

主要区别在于使用ADAL你会使用AuthenticationContext来获取令牌,而在MSAL中你可以使用ConfidentialClientApplicationPublicClientApplication,具体取决于应用程序是在后端运行还是在用户的设备上运行。

这是关于Differences between ADAL.NET and MSAL.NET applications的文章。

当您使用MSAL.Net获取Microsoft Graph API的令牌时,您可以使用以下代码:

public static PublicClientApplication PublicClientApp = new 
PublicClientApplication(ClientId);
var app = App.PublicClientApp;
ResultText.Text = string.Empty;
TokenInfoText.Text = string.Empty;
var accounts = await app.GetAccountsAsync();
authResult = await app.AcquireTokenSilentAsync(_scopes, accounts.FirstOrDefault());

有关更多详细信息,您可以参考此article,在左侧菜单中还包括AndroidiOS

© www.soinside.com 2019 - 2024. All rights reserved.