Azure功能的身份验证

Tha*_*ole 19 authentication azure azure-functions

在过去的24小时里,我一直在阅读有关如何创建Azure功能的所有内容,并已成功将MVC WebApi转换为具有多个功能的新功能应用程序.我的问题是我没有找到任何关于如何使用它们进行最基本的身份验证的明确文档或教程.

我的情景很简单.在我的AAD中配置用户,然后授予这些用户访问特定功能的权限.网站上的用户将点击UI元素,这些元素又会触发调用我的Azure功能的Javascript.在函数中,我需要能够以某种方式验证它们的身份,因为我将把它传递给与SQL实例交互的其他函数.

有人可以指点我的文档,文章,一个例子,一些东西,这表明我如何实现这一目标?

为了记录,我在门户网站上找到了我的功能应用程序的"身份验证"配置,并选择了AAD作为我的身份验证提供程序.我已经添加了我的功能应用程序并配置了一些用户.然后我编写了以下测试函数:

[FunctionName("GetThings")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.User, "GET", Route = null)]HttpRequestMessage req, TraceWriter log)
{
    log.Info("Getting all the things");
    var identity = ClaimsPrincipal.Current.Identity;

    return identity.IsAuthenticated ?
        req.CreateResponse(HttpStatusCode.Unauthorized, "Not authenticated!") :
        req.CreateResponse(HttpStatusCode.OK, $"Hi {identity.Name}!");
}
Run Code Online (Sandbox Code Playgroud)

目前,当我尝试直接点击端点时,我会被重定向到登录页面......所以我猜这部分是有效的.我如何生成/检索用户令牌,将请求发送到函数或在服务器上处理它们并不清楚.

救命?

evi*_*obu 13

用户通过Azure AD进行身份验证后,您将看到一个AppServiceAuthSessoincookie.这是一个不透明的cookie,但你可以通过电话交换索赔

https://yourFunctionApp.azurewebsites.net/.auth/me
Run Code Online (Sandbox Code Playgroud)

并作为Cookie标题传递不透明的cookie .而且,id_token你回来后很适合用作Bearer令牌.

实际上它只是看起来对我来说,我没有真正测试它作为持票人,所以有点谨慎.

获得索赔

该机制称为Easy Auth,谷歌更容易获得该名称.

有关令牌商店的更多信息,请访问
https://cgillum.tech/2016/03/07/app-service-token-store/

...说你可以通过阅读用户浏览器中的HTTP标头来获取声明:

访问令牌

在后端代码中,访问这些令牌就像读取HTTP请求头一样简单.标题命名为X-MS-TOKEN-{provider}-{type}.下面列出了可能的令牌头名称:

Azure Active Directory令牌请求标头:

X-MS-TOKEN-AAD-ID-TOKEN
X-MS-TOKEN-AAD-ACCESS-TOKEN
X-MS-TOKEN-AAD-EXPIRES-ON
X-MS-TOKEN-AAD-REFRESH-TOKEN
Run Code Online (Sandbox Code Playgroud)

我其实刚刚发现,所以谢谢你的问题!

更新:

我的预感是正确的,id_token作为持票人也很好:

$ curl -isk https://{funcApp}.azurewebsites.net/api/{someFunc} \
       -H "Authorization: Bearer eyJ0eXAiOi....oEU-Q"

HTTP/1.1 200 OK
Cache-Control: no-cache
Server: Microsoft-IIS/8.0
...
Run Code Online (Sandbox Code Playgroud)

两种阅读方式(阅读标题与/.auth/me后端用用户的Cookie 调用)之间的主要区别在于您获得的详细信息量.后者的方式更多.

以下是您通过Easy Auth获取Twitter身份验证用户的标题集:

{
   "cookie": "AppServiceAuthSession=Lx43...xHDTA==",
   ...
   "x-ms-client-principal-name": "evilSnobu",
   "x-ms-client-principal-id": "35....",
   "x-ms-client-principal-idp": "twitter",
   "x-ms-token-twitter-access-token": "35...Dj",
   "x-ms-token-twitter-access-token-secret": "OK3...Jx",
}
Run Code Online (Sandbox Code Playgroud)

以及通过致电获得的索赔/.auth/me:

{
   "access_token": "35...FDj",
   "access_token_secret": "OK3...sJx",
   "provider_name": "twitter",
   "user_claims": [
      {
         "typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
         "val": "352660979"
      },
      {
         "typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn",
         "val": "evilSnobu"
      },
      {
         "typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
         "val": "Safarihat Hacker"
      },
      {
         "typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/webpage",
         "val": "..."
      },
      {
         "typ": "urn:twitter:description",
         "val": "GENIUS. HAVE BRAIN. WILL TRAVEL."
      },
      {
         "typ": "urn:twitter:location",
         "val": ""
      },
      {
         "typ": "urn:twitter:time_zone",
         "val": "London"
      },
      {
         "typ": "urn:twitter:lang",
         "val": "en"
      },
      {
         "typ": "urn:twitter:verified",
         "val": "False"
      },
      {
         "typ": "urn:twitter:profile_image_url_https",
         "val": "https://pbs.twimg.com/profile_images/867473646876545024/1elebfK1_normal.jpg"
      }
   ],
   "user_id": "evilSnobu"
}
Run Code Online (Sandbox Code Playgroud)