gpt4 book ai didi

Azure API 应用访问 token (ADAL) 不起作用

转载 作者:行者123 更新时间:2023-12-02 06:33:10 24 4
gpt4 key购买 nike

我已经创建了一个 API(Azure API 应用程序),并使用 Azure Active Directory 启用了身份验证/授权(来自 APP API)。应用程序服务已在 AAD 中注册,到目前为止一切看起来都很好。

我已按照下面帖子中的步骤生成 token ,但 token 似乎不起作用。

var authContext = new AuthenticationContext("https://login.microsoftonline.com/<guid>/oauth2/authorize");
var credential = new ClientCredential("<clientId>", "<secret_from_aad>");
var result = (AuthenticationResult)authContext.AcquireTokenAsync("http://<api>.azurewebsites.net", credential).Result;
var token = result.AccessToken;

https://msdn.microsoft.com/en-us/library/azure/mt428036.aspx

原始请求如下所示:

GET https://<api>.azurewebsites.net/rest/v1/crm/surveys/all HTTP/1.1
Host: <api>.azurewebsites.net
Connection: close
Accept-Encoding: gzip,deflate
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ<rest-of-token...>
Host: <api>.azurewebsites.net
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

响应是

HTTP/1.1 401 Unauthorized
Content-Length: 58
Content-Type: text/html
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="<api>.azurewebsites.net"
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=dd32cab21d0ca9541343a77c51d355d0781c0e0a4147a2166ecb955fe9d94a60;Path=/;Domain=<api>.azurewebsites.net
Date: Fri, 11 Nov 2016 11:20:49 GMT
Connection: close

You do not have permission to view this directory or page.

我花了一段时间寻找一种在不显示登录页面的情况下生成 token 的方法,但我不确定这是最好的方法(虽然很简单......)。

使用 API 的系统必须能够以编程方式生成 token 并随请求一起发送。

我创建了一个虚拟 AAD、一个虚拟应用服务等,并将此代码示例放在一起:

class Program
{
static void Main(string[] args)
{
string response = HttpRequest();
Console.ReadLine();
}

public static string HttpRequest()
{
string serviceURI = "https://apiappdemo.azurewebsites.net/api/values/";

//Get the access token
string token = GetToken();

HttpWebRequest request = System.Net.WebRequest.Create(serviceURI) as System.Net.HttpWebRequest;
request.KeepAlive = true;
request.Method = "GET";
request.ContentLength = 0;
request.ContentType = "application/json";
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse) //Failing here... 401
{
using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
}

public static string GetToken()
{
var authContext = new AuthenticationContext("https://login.microsoftonline.com/b37d6c4c-012f-450c-81c7-406b6b584348/oauth2/authorize");
var credential = new ClientCredential("7ed0dccb-ade7-4a5e-b286-32b66eb929d1", "1bVIoJyMHsbuYsfuJ7or6krbKvWw3kpKfp69jsQuilw=");
var result = (AuthenticationResult)authContext.AcquireTokenAsync("https://apiappdemo.azurewebsites.net", credential).Result;
return result.AccessToken;
}
}

有什么想法可能是错误的吗?

问候,迈克

最佳答案

为了使 token 适用于受 Azure AD 保护的应用服务,我们需要使用高级托管模式(请参阅 here )。

我们将在 Azure AD 中使用两个应用程序注册器,第一个用于保护应用程序服务。第二个用作客户端来请求资源。我们需要指定允许的 token 受众,即应用程序的应用程序 id URI(第一个应用程序)。这是一个图供您引用: enter image description here

然后我们可以使用第二个应用程序作为客户端来获取为应用程序服务配置的应用程序的 token 。并确保您在 AcquireTokenAsync 方法中指定的资源是预览设置中的 audience 配置。

关于Azure API 应用访问 token (ADAL) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com