简体   繁体   English

在 LocalHost 上使用 Azure Functions 配置身份验证

[英]Configure Authentication with Azure Functions on LocalHost

I'd like to use Visual Studio 2017 to build Azure Function App with HttpTrigger.我想使用 Visual Studio 2017 来构建带有 HttpTrigger 的 Azure Function App。 However, I can't find the way how I could add Azure Active Directory authentication to secure the end point.但是,我找不到如何添加 Azure Active Directory 身份验证来保护端点的方法。

Will it work if I just add jwt token to the http request and then call ClaimsPrincipal.Current.Claims inside the method?如果我只是将 jwt 令牌添加到 http 请求,然后在方法内调用 ClaimsPrincipal.Current.Claims ,它会起作用吗? Is there any other solution?还有其他解决方案吗?

AFAIK, the authentication for the external identity data provider only can config on the Azure portal. AFAIK,外部身份数据提供者的身份验证只能在 Azure 门户上进行配置。 And if you host the Azure function on Azure, the answer is yes.如果您在 Azure 上托管 Azure 功能,答案是肯定的。 However if you host the Azure function on local, there is no way we can config for the authentication using Azure Active Directory.但是,如果您在本地托管 Azure 功能,我们将无法使用 Azure Active Directory 配置身份验证。

As a workaround, you need to get the token from headers and verify the token manually before run the function code.作为一种解决方法,您需要从标头中获取令牌并在运行函数代码之前手动验证令牌。 And if you want to support to config the authentication for host Azure function on local, you can submit the feedback from here .如果您想支持在本地配置主机 Azure 功能的身份验证,您可以从这里提交反馈。

You can use the Microsoft OpenID Connect and JWT libraries to validate the token and get claims based on a received access token.您可以使用 Microsoft OpenID Connect 和 JWT 库来验证令牌并根据收到的访问令牌获取声明。 Here's an example: https://github.com/azure-samples/ms-identity-dotnet-webapi-azurefunctions/tree/master/这是一个例子: https : //github.com/azure-samples/ms-identity-dotnet-webapi-azurefunctions/tree/master/

  1. Create an app registration in AD在 AD 中创建应用注册
  2. Issue a browser request to get an access code发出浏览器请求以获取访问代码
  3. Issue an HTTP POST request for an access token using the code and the secret via cURL使用代码和密码通过 cURL 发出访问令牌的 HTTP POST 请求
  4. Send the access token as an Authorization Bearer header to the local function endpoint将访问令牌作为 Authorization Bearer 标头发送到本地函数端点

I had issues using newer versions of Microsoft.IdentityModel.Protocols.OpenIdConnect with .NET 6.0 and Azure Functions 4 and had to fall back to version 6.10.2.我在使用较新版本的 Microsoft.IdentityModel.Protocols.OpenIdConnect 和 .NET 6.0 和 Azure Functions 4 时遇到问题,不得不回退到版本 6.10.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM