简体   繁体   English

如何检索Azure Web App上托管的.NET Core中的AD用户名?

[英]How can I retrieve AD Username in .NET Core hosted on Azure Web App?

I have used HTTPContext.User.Identity.Name to get the current users name. 我使用HTTPContext.User.Identity.Name来获取当前用户名。 It works via localhost but returns null on Azure Web App. 它通过localhost工作,但在Azure Web App上返回null。

HTTPContext.User.Identity.Name has worked before when using .Net Framework but I am now using .Net Core which doesn't work. HTTPContext.User.Identity.Name在使用.Net Framework之前已经有效,但我现在使用的.Net Core无法正常工作。

Current code: 当前代码:

private void SetUpUser()
{
    var identityName = HttpContext.User.Identity.Name;
    identityName = identityName.Replace("@example.com", "").Replace(".", "").Replace("example\\", "");
    string userName = identityName.Replace(".", "");
}

Error message: 错误信息:

NullReferenceException: Object reference not set to an instance of an object. NullReferenceException:未将对象引用设置为对象的实例。 CommissionInputApp.Controllers.HomeController.SetUpUser() in HomeController.cs HomeController.cs中的CommissionInputApp.Controllers.HomeController.SetUpUser()

Do you set the NameClaimType in OpenIdConnectOptions configuration: 您是否在OpenIdConnectOptions配置中设置NameClaimType:

options.TokenValidationParameters = new TokenValidationParameters() { NameClaimType = "name" };

I haven't create my own application & Api , just use fabrikamb2cdomain as code sample provides , even though some method is obsolete in .net core 2.2 , for example ,cache.HasStateChanged . 我没有创建自己的应用程序和Api,只是使用fabrikamb2cdomain作为代码示例提供,即使某些方法在.net core 2.2中已经过时,例如cache.HasStateChanged。 The authecation still works and get correct name with User.Identity.Name , HttpContext.User.Claims also be set : 验证仍然有效,并通过User.Identity.Name,HttpContext.User.Claims设置正确的名称:

在此输入图像描述

Also you can provide more details and code to us to reproduce the issue. 您还可以向我们提供更多详细信息和代码以重现该问题。

暂无
暂无

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

相关问题 如何在 Azure Web App 上托管的 Web 表单中检索当前的 Azure AD 用户信息(电子邮件、名字、姓氏)? - How can I retrieve current Azure AD User informations (email, firstname, lastname) in web-forms hosted on Azure Web App? 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? 无法连接托管在 Azure Web 应用程序上的安全 Asp.Net Core Web 套接字(使用 TLS) - Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS) 如何将 Azure AD 和非 Azure AD 令牌添加到同一个 .NET 核心 Z72664DC1959F3B0C047C048 - How I can add both Azure AD and non Azure AD tokens to the same .NET Core Api Azure AD B2C 声明能否用于 .NET Core Web 应用程序中的授权目的? - Can Azure AD B2C Claims be used for authorization purposes in a .NET Core Web App? 我如何在Webjob内的Azure托管ASP .NET Core应用程序的wwwroot文件夹中获取图像文件的路径 - How can i get the path to an image file in wwwroot folder of an azure hosted asp .net core app inside a webjob 如何在 .Net 核心 Web API 中进行 Azure AD 组授权? - How to do Azure AD groups authorization in .Net core web API? AWS Cognitive Hosted UI显示在asp.net核心Web应用程序中登录了用户名 - AWS Cognitive Hosted UI display signed in username in asp.net core web app 如何在Azure for .NEt核心应用程序中应用woff字体? - How can i apply woff fonts in Azure for .NEt core app? 有没有办法在 Azure 上使用托管 ASP.NET 核心 web 应用程序的本地数据库? - Is there a way to use a local db with a hosted ASP.NET core web app on Azure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM