在 Azure Functions 上启用 AD 身份验证 - Web API - 如何获取令牌来调用 API?
我有一个演示/虚假的天蓝色功能应用程序,默认情况下对任何人开放。它有一个你好世界功能。它使用“AuthorizationLevel.Anonymous” 到目前为止一切顺利 - 它是完全开放的,任何人都可以调用它。
现在我需要确保只有我们组织内的用户才能使用此 API。所以我遵循了本教程: https://learn.microsoft.com/en-us/azure/app-service/scenario-secure-app-authentication-app-service
当我尝试从浏览器调用我的演示函数时,我收到 401 未经授权的错误,我想这是一件好事 - 这就是当未经授权的用户尝试访问我的 API 时我所请求的。
根据我所读到的内容,我现在为了调用这个 API,我必须向它传递一个令牌。但这是我不清楚如何/从哪里获得此令牌的地方。
如果我创建新的应用程序注册并将“未经身份验证的请求”从 401 更改为 302 选项,则当我尝试调用 API 时,它会正确地将我重定向到 Microsoft 登录提示。我输入我的信用信息,然后该功能就起作用了。 问题是我的 API 的调用者将以编程方式执行此操作,而不是由人手动执行。
到目前为止,我已经检查了该教程中列出的所有相关链接,但还没有找到我要找的内容。或者也许我错过了。
任何提示将不胜感激。我想我只需要阅读正确的文章/文档,这将帮助我了解这个特定用例的总体情况。
谢谢。
编辑 1
如果有帮助,这里是我的集成助手向导输出的屏幕截图。我选择“web api”作为我尝试构建的应用程序类型:
I have a demo / bogus azure function app that by default is open to anyone. It has a hello world function. Its using "AuthorizationLevel.Anonymous"
So far so good - it's wide open and anyone can call it.
Now I need to ensure that only users within our organization can consume this API. So I followed this tutorial: https://learn.microsoft.com/en-us/azure/app-service/scenario-secure-app-authentication-app-service
When I try to call my demo function from a browser I get a 401 unauthorized error which is a good thing I guess - that's what I requested when unauthorized users try to hit my API.
From what I've read, I in order to call this API now, I have to pass it a token. But this is where it's not clear to me how / where I get this token.
If I create the new application registration and change the "Unauthenticated requests" from the 401 to 302 option, it correctly redirects me to the Microsoft Login prompt when I try to call my API. I enter my creds and then the function works.
The issue is that the callers of my API will be doing so programmatically and not manually by a person.
So far I've been checked all the related links listed in that tutorial but I haven't found what I'm looking for. Or maybe I missed it.
Any tips would be appreciated. I think I just need to read the right article / doc that will help me understand the big picture for this specific use case.
Thanks.
EDIT 1
In case it helps, here's a screen shot of the output from my integration assistant wizard. I selected "web api" as the type of application I'm trying to build:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您发布的文档中:
“在应用服务身份验证设置部分中,将身份验证设置为需要身份验证,将未经身份验证的请求设置为 HTTP 302 发现重定向:建议用于网站。”
如果这样设置,则该网站有问题。
From the documentation you posted:
"In the App Service authentication settings section, leave Authentication set to Require authentication and Unauthenticated requests set to HTTP 302 Found redirect: recommended for websites."
If that's set then there's something wrong with the website.