根据此文档:
ASP.NET Core 2.1 Documentation
我应该能够使用ControllerBase.User或HttpContext.User访问用户身份。
根据上面的链接:
“从应用程序的DI服务集合中获取当前用户的身份也更具可测性,因为可以轻松注入测试身份。”
据我所知,我应该在Startup.cs中使用依赖注入或者在控制器的构造函数中注入身份信息。
然而,经过无休止的调查后,我无法找到该程序。
以下步骤描述了我的方案。我希望有人可以指导我朝着正确的方向前进。
1)使用Visual Studio 2017,使用以下选项创建ASPNET.core Web API项目:
2)使用以下代码将AuthDetailsController.cs添加到Controllers文件夹:
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace CoreWebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthDetailsController : Controller
{
[HttpGet]
public JsonResult Get()
{
var r = new Dictionary<String, Object>();
r.Add("User.Identities", User.Identities);
r.Add("User.Claims", User.Claims);
r.Add("HttpContext.User.Identities", HttpContext.User.Identities);
r.Add("HttpContext.User.Claims", HttpContext.User.Claims);
return Json(r );
}
}
}
3)作为新应用程序部署到Azure
4)导航到https://[site_name].azurewebsites.net/api/AuthDetails
5)没有关于用户身份的信息,因为访问是匿名的:
{"User.Identities":[{"authenticationType":null,"isAuthenticated":false,"actor":null,"bootstrapContext":null,"claims":[],"label":null,"name":null,"nameClaimType":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name","roleClaimType":"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"}],"User.Claims":[],"HttpContext.User.Identities":[{"authenticationType":null,"isAuthenticated":false,"actor":null,"bootstrapContext":null,"claims":[],"label":null,"name":null,"nameClaimType":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name","roleClaimType":"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"}],"HttpContext.User.Claims":[]}
6)使用以下选项启用Azure身份验证:
7)再打开https://[site_name].azurewebsites.net/api/AuthDetails
8)您被重定向到登录页面
9)成功登录后,ControllerBase.User和HttpContext.User中都没有身份信息。响应与启用身份验证之前的响应相同。
您必须告诉需要在startup.cs中使用的身份验证。
它会是这样的。
services.AddAuthentication(IISDefaults.AuthenticationScheme);
有关此详细信息的链接可在this上找到。
此外,对于控制器中的操作,您需要添加[授权]。只有添加此项后,您才能检索详细信息。如果在控制器级别添加,对于不需要身份验证的操作,可以添加[AllowAnonymous]
[HttpGet]
[Authorize]
public JsonResult Get()
{
var r = new Dictionary<String, Object>();
r.Add("User.Identities", User.Identities);
r.Add("User.Claims", User.Claims);
r.Add("HttpContext.User.Identities", HttpContext.User.Identities);
r.Add("HttpContext.User.Claims", HttpContext.User.Claims);
return Json(r );
}
这个例子有效:
https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2
我可以使用以下方法识别控制器内的用户:
User.FindAll("preferred_username").First().Value;
这是一个不同的项目类型。它是一个MVC网站而不是API项目。
这不是我想要的,但至少它是有效的。我将研究如何将其应用于我的API项目。
我做到了 !!!
这个documentation指出了我正确的方向。
这些是重现我的场景的步骤:
1)使用Visual Studio 2017,使用以下选项创建ASPNET.core Web API项目:
2)使用以下代码将AuthDetailsController.cs添加到Controllers文件夹:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace CoreWebApiTest2.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthDetailsController : ControllerBase
{
// GET: api/AuthDetails
[HttpGet]
public Dictionary<string,string> Get()
{
return new Dictionary<string, string> { {"X-MS-CLIENT-PRINCIPAL-NAME", Request.Headers["X-MS-CLIENT-PRINCIPAL-NAME"] },
{"X-MS-CLIENT-PRINCIPAL-ID", Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"] } };
}
}
}
3)作为新应用程序部署到Azure
4)使用以下选项启用Azure身份验证:
5)打开https://[site_name].azurewebsites.net/api/AuthDetails
6)您被重定向到登录页面
7)成功登录后,您会收到回复:
{"X-MS-CLIENT-PRINCIPAL-NAME":"your_user@your_domain","X-MS-CLIENT-PRINCIPAL-ID":"your_user_ad_guid"}