我有一个 Azure API 应用程序,已将其设置为使用 Azure AD 身份验证。 就像这样:
我还有一个控制台应用程序,它为我的 API 应用程序生成了客户端 API,如下所示:
如果我在 API 应用程序上禁用身份验证,我可以从控制台应用程序调用 API。 如果我启用Azure AD身份验证,客户端将因“未授权”异常而无法调用API,这当然是预料之中的。
我到处搜索以查找有关如何向 API 提供正确类型的凭据的任何信息。
有一个 client.Credentials
属性,可以是 TokenCredentials
或 BasicAuthenticationCredentials
如果我有 AD 用户的用户名和密码,通过 Azure AD 对客户端进行身份验证的正确方法是什么?
我无法找到与从 Azure SDK 自动生成的 API 客户端相关的任何文档。
[编辑] @chrisgillum 的回复引出了一篇有关如何执行此操作的文章:
internal class Program
{
[STAThread]
private static void Main(string[] args)
{
var uri = new Uri("https://ro....azureapp.azurewebsites.net");
var client = new LearningAzure(uri);
client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
ServicePrincipal.GetS2SAccessTokenForProdMSA().AccessToken);
var res = client.Foo.GetById(123);
}
}
public static class ServicePrincipal
{
// The _authority is the issuer URL of the tenant.
private static readonly string _authority = "https://login.windows.net/ro....ouse.onmicrosoft.com";
// The _resource is the Client ID of the "data web api" AAD app.
private static readonly string _resource = "b8b0.....8e3623d";
// The Client ID of the "client" AAD app (i.e. this app).
private static readonly string _clientId = "d25892.....33a0";
// The key that was created for the "client" app (i.e. this app).
private static readonly string _clientSecret = "??????";
public static AuthenticationResult GetS2SAccessTokenForProdMSA()
{
return GetS2SAccessToken(_authority, _resource, _clientId, _clientSecret);
}
/// <summary>
/// Gets an application token used for service-to-service (S2S) API calls.
/// </summary>
private static AuthenticationResult GetS2SAccessToken(string authority, string resource, string clientId,
string clientSecret)
{
// Client credential consists of the "client" AAD web application's Client ID
// and the key that was generated for the application in the AAD Azure portal extension.
var clientCredential = new ClientCredential(clientId, clientSecret);
// The authentication context represents the AAD directory.
var context = new AuthenticationContext(authority, false);
// Fetch an access token from AAD.
var authenticationResult = context.AcquireToken(
resource,
clientCredential);
return authenticationResult;
}
}
_clientSecret
我如何获得这个?
我的 Azure AD 中配置的客户端应用程序没有任何用于生成 key 的选项。
已发布的网络应用程序可以,但我自定义创建的 native 应用程序在配置页面上只有一个客户端 ID。 有想法吗?
最佳答案
要在控制台应用程序上获取凭据,如果提示用户登录,则可以使用他们的凭据来获取 token 。否则,您需要使用客户端应用程序 key (客户端应用程序 URI 和匹配的 API key )来获取 token 。
Uri aadUri = new Uri(_settings.AadUri);
Uri authorityUri = new Uri(aadUri, _settings.TenantUri);
string authority = authorityUri.ToString();
AuthenticationResult authorizationResult;
var authenticationContext = new AuthenticationContext(authority, new TokenCache());
var clientCredential = new ClientCredential(_settings.ClientId, _settings.AppKey);
authorizationResult = authenticationContext.AcquireToken(_settings.AppIdUri, clientCredential);
您还需要确保您的应用 URI 位于服务端点的 list 中(或者以某种方式规定让您的应用在 Azure 中访问 Azure AD)。
关于api - 访问经过身份验证的Azure API APP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494623/