我需要添加0Auth到我的Azure Function应用程序,这将通过HTTP请求,POST触发,我应该检索用户的详细信息,我传递到主体凭据.我是一个相当新手的编码,和所有的教程,我已经看到了,由于我的unexperience,对我来说,这只是一堆没有意义的代码,其中有许多细节,在我看来,只有有经验的人才能理解。我可以生成令牌,但不能在请求中一起传递,而且当我调用API时,它会向我抛出以下错误:
Parameters: Connection String: [No connection string specified], Resource: http://localhost:7235/api/.default, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. ERROR: AADSTS500011: The resource principal named http://localhost:7235/api/.default was not found in the tenant named <CN>. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.
作为一个令牌提供者,我使用类AzureServiceTokenProvider();
,我相信这就是导致这个错误的原因。我寻找了一些令牌提供者的替代方法,但我只能找到TokenProver();
,但我尝试使用其中的一些,也得到了错误。我被要求使用C#来做所有的事情,其他什么都不做。不是门户Azure。我唯一能从Azure使用的东西是Azure存储帐户。我能够创建“CRUD”系统,它们工作得很好。我没有更多与令牌相关的类。
下面是我的代码:
public static async Task<IActionResult> Login(
[HttpTrigger(AuthorizationLevel.Admin, "POST", Route = "login")] HttpRequest req,
[Table("User", Connection = "AzureWebJobsStorage")] TableClient tdClient,
ILogger log)
{
string url = "http://localhost:7235/api/.default";
var httpClient = new HttpClient();
var azureServiceTokenProvider = new AzureServiceTokenProvider();
string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(url);
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<CreateUserDto>(requestBody);
var user = new User
{
email = input.email,
password = input.password
};
string userToString = user.ToString();
var stringContent = new StringContent(userToString, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return new OkObjectResult(response);
}
我真的不想使用AzureServiceTokenProvider();
类。是否可以使用TokenProvider();
来实现这一点。我该如何实现这一点?我希望我已经说清楚了。谢谢。
1条答案
按热度按时间ie3xauqp1#
如果您希望强制执行身份验证,则应考虑Azure功能/应用服务身份验证或
https://learn.microsoft.com/en-us/azure/azure-functions/security-concepts?tabs=v4#enable-app-service-authenticationauthorization