This is not a duplicate question ( as far as I researched ). My problem is everything works nice when I run ASP.NET Core 2.1 MVC application in VS 2019 but when I deploy to local IIS and launch my application in browser it is not working. My operating system is Windows 10.
I have below turned on in IIS
I am doing below to get username of person who launched my ASP.NET Core 2.1 MVC application in browser.
In Properties -> launchSettings.json
"windowsAuthentication": true,
"anonymousAuthentication": false,
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
In Helper.cs
public static string GetUserName(IHttpContextAccessor httpContextAccessor)
{
string userName = string.Empty;
if(httpContextAccessor != null &&
httpContextAccessor.HttpContext != null &&
httpContextAccessor.HttpContext.User != null &&
httpContextAccessor.HttpContext.User.Identity != null)
{
userName = httpContextAccessor.HttpContext.User.Identity.Name;
string[] usernames = userName.Split('\\');
userName = usernames[1].ToUpper();
}
return userName;
}
In MyController.cs
public class MyController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
[HttpPost]
public JsonResult CreateOrder([FromBody] OrderModel model)
{
string username = Helper.GetUserName(_httpContextAccessor);
.....
}
}
When I run my application in VS 2019 everything is fine but when I publish my application to IIS and launch my application in browser I get error due to httpContextAccessor.HttpContext.User.Identity.Name null.
I also tried various ways like FindFirst using Claims but all those work good when I run in VS 2019 but not publish my application to IIS and launch my application in browser. What mistake I am doing.
Thanks in advance.