Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Azure身份验证在Azure功能中获取当前用户身份?_C#_Azure_Azure Active Directory_Azure Functions_Azure Authentication - Fatal编程技术网

C# 如何使用Azure身份验证在Azure功能中获取当前用户身份?

C# 如何使用Azure身份验证在Azure功能中获取当前用户身份?,c#,azure,azure-active-directory,azure-functions,azure-authentication,C#,Azure,Azure Active Directory,Azure Functions,Azure Authentication,我创建了一个新的功能应用程序,为其启用了应用程序服务身份验证/授权(“使用身份验证/授权来保护您的应用程序并处理每用户数据”),并禁用了未经身份验证的请求 到目前为止,一切似乎都运转正常。如果我试图请求我的HttpTriggered函数,它要求我首先登录;一旦我登录,所有请求都会按应有的方式处理。因此,“保护您的应用程序”部分没有问题 然而,我完全被“处理每个用户的数据”部分所困扰。My Azure函数被调用为 public static async Task<HttpResponseMe

我创建了一个新的功能应用程序,为其启用了应用程序服务身份验证/授权(“使用身份验证/授权来保护您的应用程序并处理每用户数据”),并禁用了未经身份验证的请求

到目前为止,一切似乎都运转正常。如果我试图请求我的
HttpTrigger
ed函数,它要求我首先登录;一旦我登录,所有请求都会按应有的方式处理。因此,“保护您的应用程序”部分没有问题

然而,我完全被“处理每个用户的数据”部分所困扰。My Azure函数被调用为

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequestMessage请求,TraceWriter日志)
而且在
HttpRequestMessage
中没有与身份验证相关的内容。(AuthorizationLevel.Anonymous似乎控制着完全不同的事情——即,函数是否可以由任何人调用,或者是否只能由具有固定API密钥的人调用)


如何获取调用函数的经过身份验证的用户的身份?

似乎可以从全局状态
System.Security.Claims.ClaimsPrincipal.current.identity.name获取当前用户名(我最初发布此问题时不知道)。然而,目前尚不清楚这是否是一种可靠或推荐的登录用户信息的方法

例如:

using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp
{
    public static class Function1
    {
        [FunctionName("HttpTriggerCSharp")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            return req.CreateResponse(HttpStatusCode.OK, "Hello " + ClaimsPrincipal.Current.Identity.Name);
        }
    }
}
使用System.Net;
使用System.Net.Http;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.Azure.WebJobs.Host;
命名空间函数
{
公共静态类函数1
{
[函数名(“HttpTriggerCSharp”)]
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequestMessage请求,TraceWriter日志)
{
返回req.CreateResponse(HttpStatusCode.OK,“Hello”+ClaimsPrincipal.Current.Identity.Name);
}
}
}

与应用服务EasyAuth的一流集成尚未到位,但我们正在回购中进行跟踪。我们正在研究这个问题,很快就会在这方面有所改进


正如您所发现的,您可以启用EasyAuth,它需要登录,并将经过身份验证的主体通过,允许您通过标准的.NET API(如ClaimsPrincipal.Current等)访问它。但是问题是,您必须使用身份验证级别的匿名标记您的方法,这不是您想要的,并要求您在方法中拒绝未经验证的请求(请参阅上述问题中的注释)


我们将在即将发布的版本中解决所有这些问题。

从逻辑上讲,是授权级别。Anonymous不会提供当前的索赔主体

不幸的是,仅更改为AuthorizationLevel。函数也没有帮助: 对我来说,我发现ClaimsPrincipal.Current为null,即使在通过Azure Active Directory B2C成功验证之后也是如此

最后,我尝试了AuthorizationLevel.User,但azure函数目前不支持它,请参见


我相信您需要按照上接受的答案(我现在正在尝试…)遵循这些步骤

复制自我的问题,该问题旨在找到如何使用Azure Active Directory身份验证用户进行本地调试。

这将适用于在Azure上使用Azure功能配置的任何提供程序,同时适用于本地调试和部署场景


好吧,再过几个小时(好吧,很多)后,我现在已经找到了一个解决方案。这在本地和部署场景中都有效。我在这里发布了一个模板解决方案:

以下是概述整个流程的步骤:

  • 在https://
    函数名
    .azurewebsites.net上登录您的函数
  • Chrome->Application->Cookies->->->AppServiceAuthSession->复制值中的CTRL-SHIFT-C
  • 打开
    local.settings.json
    并粘贴
    AuthenticationToken
    设置中上一步的值
  • 在那里,从
    AuthenticationBaseAddress
  • 启动应用程序
  • 祈祷吧
  • 享受魔法(希望如此)
  • 以下是主要事件:

    public static class AuthenticationExtensions
    {
        public static Authentication Authenticate(this HttpRequest @this)
        {
            var handler = new HttpClientHandler();
            var client = new HttpClient(handler) // Will want to make this a singleton.  Do not use in production environment.
            {
                BaseAddress = new Uri(Environment.GetEnvironmentVariable("AuthenticationBaseAddress") ?? new Uri(@this.GetDisplayUrl()).GetLeftPart(UriPartial.Authority))
            };
            handler.CookieContainer.Add(client.BaseAddress, new Cookie("AppServiceAuthSession", @this.Cookies["AppServiceAuthSession"] ?? Environment.GetEnvironmentVariable("AuthenticationToken")));
    
            var service = RestService.For<IAuthentication>(client);
    
            var result = service.GetCurrentAuthentication().Result.SingleOrDefault();
            return result;
        }
    }
    
    使用,可以从
    Run
    方法中注入的实例检索:

    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
        HttpRequest httpRequest, 
        ILogger logger, 
        ClaimsPrincipal claimsPrincipal)
     {
                // Explores the authenticated user's claims in claimsPrincipal.
     }
    
    公共静态异步任务运行(
    [HttpTrigger(AuthorizationLevel.Anonymous,“获取”、“发布”,路由=null)]
    HttpRequest HttpRequest,
    ILogger记录器,
    索赔人(索赔人)
    {
    //在claimsPrincipal中探索经过身份验证的用户的声明。
    }
    
    您的“使用身份验证/授权来保护您的应用程序并处理每用户数据”是指一篇文章吗?我找不到那样精确的title@woelliJ,它指的是Azure portal中“身份验证/授权”按钮的提示。这里也有同样的问题,我有一个想法,可以让它与API管理一起工作?“问题是,您必须使用身份验证级别匿名标记您的方法,这不是您想要的”似乎AuthenticationLevel是用于完全不同的东西。我不认为这在我的情况下是个问题;用AuthenticationLevel.Anonymous标记我的方法有什么问题?如果我不想要求用户提供固定的API密钥?“并要求您拒绝方法中未经验证的请求”现在,我不会拒绝方法中未经验证的请求,因为Azure似乎是为我做的(因为我在可以启用AAD/Facebook/etc身份验证的同一页面上禁用了未经身份验证的请求)。你似乎暗示未经身份验证的用户仍然可以通过某种方式使用我的功能?是的,所以这个问题确实指向了正确的方向。我非常困惑
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
        HttpRequest httpRequest, 
        ILogger logger, 
        ClaimsPrincipal claimsPrincipal)
     {
                // Explores the authenticated user's claims in claimsPrincipal.
     }