[英]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.