【发布时间】:2019-05-22 11:36:23
【问题描述】:
我使用 HTTPContext.User.Identity.Name 来获取当前用户名。它通过 localhost 工作,但在 Azure Web App 上返回 null。
HTTPContext.User.Identity.Name 之前在使用 .Net Framework 时有效,但我现在使用的 .Net Core 无效。
当前代码:
private void SetUpUser()
{
var identityName = HttpContext.User.Identity.Name;
identityName = identityName.Replace("@example.com", "").Replace(".", "").Replace("example\\", "");
string userName = identityName.Replace(".", "");
}
错误信息:
NullReferenceException:对象引用未设置为对象的实例。 HomeController.cs 中的 CommissionInputApp.Controllers.HomeController.SetUpUser()
【问题讨论】:
-
您是如何配置身份验证的?
-
Azure Active Directory 使用我们的内部 AD(内部应用程序)
-
可以在配置AAD认证的地方添加代码吗?
-
不知道你的意思,我们在之前的应用中使用过HTTPContext.User.Identity.Name,这是第一次部署时它不起作用
-
必须在某处配置 Azure AD 身份验证,无论是在代码中还是通过应用服务上的身份验证/授权。应用服务未加入您的域(也不能加入),因此常规 Windows 身份验证在那里不起作用。
标签: c# azure .net-core azure-active-directory windows-authentication