Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure AD从身份验证结果对象获取访问令牌_C#_Azure_Adal - Fatal编程技术网

C# Azure AD从身份验证结果对象获取访问令牌

C# Azure AD从身份验证结果对象获取访问令牌,c#,azure,adal,C#,Azure,Adal,我是Azure广告的新手,尝试使用广告保护的api。我已成功创建并保护了api,但在我的windows窗体应用程序中很难使用它。我在尝试了文档,但在这一行中出现了编译时错误 AuthenticationResult ar = ac.AcquireToken("https://cloudidentity.net/WindowsAzureADWebAPITest", "a4836f83-0f69-48ed-aa2b-88d0aed69652", new Uri("https://cloudident

我是Azure广告的新手,尝试使用广告保护的api。我已成功创建并保护了api,但在我的windows窗体应用程序中很难使用它。我在尝试了文档,但在这一行中出现了编译时错误

AuthenticationResult ar =
ac.AcquireToken("https://cloudidentity.net/WindowsAzureADWebAPITest",
"a4836f83-0f69-48ed-aa2b-88d0aed69652",
new Uri("https://cloudidentity.net/myWebAPItestclient"));
string authHeader = ar.CreateAuthorizationHeader();
目前ADAL中没有这种方法。有一个异步版本,我尝试过,但采用不同的参数

AuthenticationResult ar =
ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
"a4836f83-0f69-48ed-aa2b-88d0aed69652",
new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters);
除了其他信息外,它还需要IPlatformParameters对象,我对此一无所知。我试图传递null并继续,但这一行出现了错误

AuthenticationResult ar =
ac.AcquireToken("https://cloudidentity.net/WindowsAzureADWebAPITest",
"a4836f83-0f69-48ed-aa2b-88d0aed69652",
new Uri("https://cloudidentity.net/myWebAPItestclient"));
string authHeader = ar.CreateAuthorizationHeader();
错误在于ADAL中没有针对ar对象的此类方法。当他也在使用windows窗体应用程序时,我跳到了这一步。他写的代码是

Task<AuthenticationResult> ar = authContext.AcquireTokenAsync("https://carsforher.onmicrosoft.com/SecuredCars_20160722021100", "2640aca3-a35e-42f8-8f6d-2e5fe1a09df4", new Uri("http://localhost"), null);
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ar.AccessToken).....

任务,但他们也编写了完全相同的代码,不幸的是,这不起作用。我使用的ADAL版本是3.12.0.827。请帮助我弄清楚如何获得访问令牌并使用api

您错误地使用AcquireTokenAsync:AcquireTokenAsync返回一个任务,而不是AuthenticationResult对象,因此方法“CreateAuthorizationHeader”和属性“AccessToken”实际上“丢失”

您的代码的固定版本将是:

AuthenticationResult ar = ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
    "a4836f83-0f69-48ed-aa2b-88d0aed69652",
    new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters).Result;

string authHeader = ar.CreateAuthorizationHeader();
string accessToken = ar.AccessToken;
或者或者,您的代码将真正运行async,您可以将“async”添加到方法签名中,并执行以下操作:

AuthenticationResult ar = await ac.AcquireTokenAsync("https://cloudidentity.net/WindowsAzureADWebAPITest",
    "a4836f83-0f69-48ed-aa2b-88d0aed69652",
    new Uri("https://cloudidentity.net/myWebAPItestclient"), IPlatformParameters);

string authHeader = ar.CreateAuthorizationHeader();
string accessToken = ar.AccessToken;