1

我的应用程序获得了一个令牌,并且可以像搜索一样对 ADC 进行 api 调用。但是新资产注册请求失败,因为“LastRegisterdBy”字段不能为空/空,并且必须与当前用户对应。但是,令牌不包含任何用户信息(AccessToken().Result.UserInfo.DisplayableId 为空)。

我主要遵循 MS 提供的入门入门项目(https://github.com/Azure-Samples/data-catalog-dotnet-get-started/blob/master/Program.cs

但我用

AcquireTokenAsync(resourceUri, clientCredential).ConfigureAwait(false)

代替

AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Always)).

这样就没有人必须输入他的凭据。目标是运行 ssis 包中的代码,它将每周执行一次以自动捕获数据中的任何更新。

这是我的代码大纲:

    class Program
        {
            static string clientIDFromAzureAppRegistration = "";
            static string clientSecret = "";
            static AuthenticationResult authResult = null;
            static string catalogName = "catalog";
            static void Main(string[] args)
            {
                var authResult = AccessToken();
                string upn = authResult.Result.UserInfo.DisplayableId;
                var id = RegisterDataAsset(authResult, SampleJson("test", upn));
            }
                static async Task<AuthenticationResult> AccessToken()
        {
            if (authResult == null)
            {
                //Resource Uri for Data Catalog API
                string resourceUri = "https://api.azuredatacatalog.com";

                //To learn how to register a client app and get a Client ID, see https://msdn.microsoft.com/en-us/library/azure/mt403303.aspx#clientID   
                string clientId = clientIDFromAzureAppRegistration;
                string clientS = clientSecret;

                // Create an instance of AuthenticationContext to acquire an Azure access token

                var authority = "https://login.microsoftonline.com/52497ec2-0945-4f55-8021-79766363dd96";
                var authContext = new AuthenticationContext(authority);
                var clientCredential = new ClientCredential(clientId, clientS);

                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                //  AcquireToken takes a Client Id that Azure AD creates when you register your client app.
                authResult = await authContext.AcquireTokenAsync(resourceUri, clientCredential).ConfigureAwait(false);
            }
            return authResult;
        }

     static string RegisterDataAsset(Task<AuthenticationResult> authResult, string json){
    ...
    }
    static HttpWebResponse SetRequestAndGetResponse(HttpWebRequest request, Task<AuthenticationResult> authResult, string payload = null){
    ...
    }
    static string SampleJson(string name, string upn){
    ...}

使用 upn = authResult.Result.UserInfo.DisplayableId; 我得到:

{"error":{"code":"InvalidPropertyValue","message":"属性之一的输入值无效。路径:'properties.lastRegisteredBy.upn'。详细信息:值不能为空、空或完全由空格。"}}

机智 upn = "test@user":

{"error":{"code":"InvalidLastRegisteredBy","message":"LastRegisteredBy 与当前用户不同。"}}

4

1 回答 1

0

我找到了解决方案,它非常简单:

应用的用户名是:clientIDFromAzureAppRegistration + "@" +tenantId。

于 2019-11-01T14:15:27.073 回答