.Net Core 2.1 getting client machines windows user name












1














I am attempting to get the currently logged in user in Windows on the clients computer. I have tried using User.Identity.Name & WindowsIdentity.GetCurrent().Name. When I run the application locally through Visual Studio, it runs great. Meanwhile when I publish this same code to our IIS server when I run the this code, it returns "NT AUTHORITYNETWORK SERVICE".



I thought this was going to be an easier task and started going through a bunch of different google searches with no luck (maybe its just that im new to .net core and am making a dumb mistake somewhere). Anyhow my server has Windows Authorization enabled (see image below).



Windows Authentication IIS



The project is a .net core 2.1 web application and I am working in C#. Any help with this would be great!



Note



My previous thread (now deleted) for this question was marked as a duplicate question (How get current user in asp.net core) I do not understand how this is a duplicate when in this question they are saying that their HttpContext is null. I do not have this issue, I return a user but it is NOT the client machine's user. Due to this I had to recreate this question. If there is a duplicate answer please post it in the comments let me review if it actually is the same and if it is, then we can mark it as a duplicate. Thank you.



I also tried using:



HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value


This was suggested in the duplicate answer post but this just gives me a null reference error.



Update #1



Just thought I would also include that my server has the following roles already installed:



Windows Auth role installed



Update #2



Here is the method getting the user name, it is the index.cshtml razor page that is initially loaded.



public async Task<IActionResult> OnGetAsync()
{
ILogger log = ApplicationLogger.CreateLogger("LoginFilter");
log.LogError("Start");
log.LogError("contextIdent: " + User.Identity.Name);
log.LogError("windowsIdent: " + WindowsIdentity.GetCurrent().Name);
log.LogError("environment: " + Environment.UserName);
log.LogError("End");

return Page();
}


This outputs the following to the stdout log:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:20316
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
End


Update #3



Added:



log.LogError("findFirst: " + User.FindFirst(ClaimTypes.NameIdentifier).Value);



After adding this the log generated the following:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:35812
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.Pages.IndexModel.OnGetAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)


Update #4



I added to my Startup.cs ConfigureServices the following:



services.AddAuthentication(IISDefaults.AuthenticationScheme);


And into my index.cshtml:



var user = WindowsIdentity.GetCurrent();
WindowsIdentity.RunImpersonated(user.AccessToken, () =>
{
var impersonatedUser = WindowsIdentity.GetCurrent();
log.LogError("impersonated: " + impersonatedUser.Name);
});


As I had expected (due to the user being passed to impersonate still being "NT AUTHORITYNETWORK SERVICE") the results are as follows:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:36547
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
impersonated: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
hcIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
End
Application is shutting down...









share|improve this question
























  • are you using windows auth with .net core?
    – Daniel A. White
    Nov 20 '18 at 13:59










  • docs.microsoft.com/en-us/aspnet/core/security/authentication/…
    – Daniel A. White
    Nov 20 '18 at 13:59










  • @DanielA.White Yes I am using windows auth with .net core
    – Lord-Link
    Nov 20 '18 at 14:01










  • @Lord-Link i mean is it properly configured?
    – Daniel A. White
    Nov 20 '18 at 14:01










  • @pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
    – Lord-Link
    Nov 20 '18 at 14:02
















1














I am attempting to get the currently logged in user in Windows on the clients computer. I have tried using User.Identity.Name & WindowsIdentity.GetCurrent().Name. When I run the application locally through Visual Studio, it runs great. Meanwhile when I publish this same code to our IIS server when I run the this code, it returns "NT AUTHORITYNETWORK SERVICE".



I thought this was going to be an easier task and started going through a bunch of different google searches with no luck (maybe its just that im new to .net core and am making a dumb mistake somewhere). Anyhow my server has Windows Authorization enabled (see image below).



Windows Authentication IIS



The project is a .net core 2.1 web application and I am working in C#. Any help with this would be great!



Note



My previous thread (now deleted) for this question was marked as a duplicate question (How get current user in asp.net core) I do not understand how this is a duplicate when in this question they are saying that their HttpContext is null. I do not have this issue, I return a user but it is NOT the client machine's user. Due to this I had to recreate this question. If there is a duplicate answer please post it in the comments let me review if it actually is the same and if it is, then we can mark it as a duplicate. Thank you.



I also tried using:



HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value


This was suggested in the duplicate answer post but this just gives me a null reference error.



Update #1



Just thought I would also include that my server has the following roles already installed:



Windows Auth role installed



Update #2



Here is the method getting the user name, it is the index.cshtml razor page that is initially loaded.



public async Task<IActionResult> OnGetAsync()
{
ILogger log = ApplicationLogger.CreateLogger("LoginFilter");
log.LogError("Start");
log.LogError("contextIdent: " + User.Identity.Name);
log.LogError("windowsIdent: " + WindowsIdentity.GetCurrent().Name);
log.LogError("environment: " + Environment.UserName);
log.LogError("End");

return Page();
}


This outputs the following to the stdout log:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:20316
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
End


Update #3



Added:



log.LogError("findFirst: " + User.FindFirst(ClaimTypes.NameIdentifier).Value);



After adding this the log generated the following:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:35812
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.Pages.IndexModel.OnGetAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)


Update #4



I added to my Startup.cs ConfigureServices the following:



services.AddAuthentication(IISDefaults.AuthenticationScheme);


And into my index.cshtml:



var user = WindowsIdentity.GetCurrent();
WindowsIdentity.RunImpersonated(user.AccessToken, () =>
{
var impersonatedUser = WindowsIdentity.GetCurrent();
log.LogError("impersonated: " + impersonatedUser.Name);
});


As I had expected (due to the user being passed to impersonate still being "NT AUTHORITYNETWORK SERVICE") the results are as follows:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:36547
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
impersonated: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
hcIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
End
Application is shutting down...









share|improve this question
























  • are you using windows auth with .net core?
    – Daniel A. White
    Nov 20 '18 at 13:59










  • docs.microsoft.com/en-us/aspnet/core/security/authentication/…
    – Daniel A. White
    Nov 20 '18 at 13:59










  • @DanielA.White Yes I am using windows auth with .net core
    – Lord-Link
    Nov 20 '18 at 14:01










  • @Lord-Link i mean is it properly configured?
    – Daniel A. White
    Nov 20 '18 at 14:01










  • @pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
    – Lord-Link
    Nov 20 '18 at 14:02














1












1








1







I am attempting to get the currently logged in user in Windows on the clients computer. I have tried using User.Identity.Name & WindowsIdentity.GetCurrent().Name. When I run the application locally through Visual Studio, it runs great. Meanwhile when I publish this same code to our IIS server when I run the this code, it returns "NT AUTHORITYNETWORK SERVICE".



I thought this was going to be an easier task and started going through a bunch of different google searches with no luck (maybe its just that im new to .net core and am making a dumb mistake somewhere). Anyhow my server has Windows Authorization enabled (see image below).



Windows Authentication IIS



The project is a .net core 2.1 web application and I am working in C#. Any help with this would be great!



Note



My previous thread (now deleted) for this question was marked as a duplicate question (How get current user in asp.net core) I do not understand how this is a duplicate when in this question they are saying that their HttpContext is null. I do not have this issue, I return a user but it is NOT the client machine's user. Due to this I had to recreate this question. If there is a duplicate answer please post it in the comments let me review if it actually is the same and if it is, then we can mark it as a duplicate. Thank you.



I also tried using:



HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value


This was suggested in the duplicate answer post but this just gives me a null reference error.



Update #1



Just thought I would also include that my server has the following roles already installed:



Windows Auth role installed



Update #2



Here is the method getting the user name, it is the index.cshtml razor page that is initially loaded.



public async Task<IActionResult> OnGetAsync()
{
ILogger log = ApplicationLogger.CreateLogger("LoginFilter");
log.LogError("Start");
log.LogError("contextIdent: " + User.Identity.Name);
log.LogError("windowsIdent: " + WindowsIdentity.GetCurrent().Name);
log.LogError("environment: " + Environment.UserName);
log.LogError("End");

return Page();
}


This outputs the following to the stdout log:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:20316
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
End


Update #3



Added:



log.LogError("findFirst: " + User.FindFirst(ClaimTypes.NameIdentifier).Value);



After adding this the log generated the following:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:35812
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.Pages.IndexModel.OnGetAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)


Update #4



I added to my Startup.cs ConfigureServices the following:



services.AddAuthentication(IISDefaults.AuthenticationScheme);


And into my index.cshtml:



var user = WindowsIdentity.GetCurrent();
WindowsIdentity.RunImpersonated(user.AccessToken, () =>
{
var impersonatedUser = WindowsIdentity.GetCurrent();
log.LogError("impersonated: " + impersonatedUser.Name);
});


As I had expected (due to the user being passed to impersonate still being "NT AUTHORITYNETWORK SERVICE") the results are as follows:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:36547
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
impersonated: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
hcIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
End
Application is shutting down...









share|improve this question















I am attempting to get the currently logged in user in Windows on the clients computer. I have tried using User.Identity.Name & WindowsIdentity.GetCurrent().Name. When I run the application locally through Visual Studio, it runs great. Meanwhile when I publish this same code to our IIS server when I run the this code, it returns "NT AUTHORITYNETWORK SERVICE".



I thought this was going to be an easier task and started going through a bunch of different google searches with no luck (maybe its just that im new to .net core and am making a dumb mistake somewhere). Anyhow my server has Windows Authorization enabled (see image below).



Windows Authentication IIS



The project is a .net core 2.1 web application and I am working in C#. Any help with this would be great!



Note



My previous thread (now deleted) for this question was marked as a duplicate question (How get current user in asp.net core) I do not understand how this is a duplicate when in this question they are saying that their HttpContext is null. I do not have this issue, I return a user but it is NOT the client machine's user. Due to this I had to recreate this question. If there is a duplicate answer please post it in the comments let me review if it actually is the same and if it is, then we can mark it as a duplicate. Thank you.



I also tried using:



HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value


This was suggested in the duplicate answer post but this just gives me a null reference error.



Update #1



Just thought I would also include that my server has the following roles already installed:



Windows Auth role installed



Update #2



Here is the method getting the user name, it is the index.cshtml razor page that is initially loaded.



public async Task<IActionResult> OnGetAsync()
{
ILogger log = ApplicationLogger.CreateLogger("LoginFilter");
log.LogError("Start");
log.LogError("contextIdent: " + User.Identity.Name);
log.LogError("windowsIdent: " + WindowsIdentity.GetCurrent().Name);
log.LogError("environment: " + Environment.UserName);
log.LogError("End");

return Page();
}


This outputs the following to the stdout log:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:20316
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
End


Update #3



Added:



log.LogError("findFirst: " + User.FindFirst(ClaimTypes.NameIdentifier).Value);



After adding this the log generated the following:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:35812
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at MyApp.Pages.IndexModel.OnGetAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)


Update #4



I added to my Startup.cs ConfigureServices the following:



services.AddAuthentication(IISDefaults.AuthenticationScheme);


And into my index.cshtml:



var user = WindowsIdentity.GetCurrent();
WindowsIdentity.RunImpersonated(user.AccessToken, () =>
{
var impersonatedUser = WindowsIdentity.GetCurrent();
log.LogError("impersonated: " + impersonatedUser.Name);
});


As I had expected (due to the user being passed to impersonate still being "NT AUTHORITYNETWORK SERVICE") the results are as follows:



Hosting environment: Production
Content root path: C:inetpubwwwrootMyApp
Now listening on: http://127.0.0.1:36547
Application started. Press Ctrl+C to shut down.
fail: LoginFilter[0]
Start
fail: LoginFilter[0]
impersonated: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
contextIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
windowsIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
environment: MYENV$
fail: LoginFilter[0]
hcIdent: NT AUTHORITYNETWORK SERVICE
fail: LoginFilter[0]
End
Application is shutting down...






c# .net .net-core .net-core-2.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 20:03







Lord-Link

















asked Nov 20 '18 at 13:56









Lord-LinkLord-Link

40621132




40621132












  • are you using windows auth with .net core?
    – Daniel A. White
    Nov 20 '18 at 13:59










  • docs.microsoft.com/en-us/aspnet/core/security/authentication/…
    – Daniel A. White
    Nov 20 '18 at 13:59










  • @DanielA.White Yes I am using windows auth with .net core
    – Lord-Link
    Nov 20 '18 at 14:01










  • @Lord-Link i mean is it properly configured?
    – Daniel A. White
    Nov 20 '18 at 14:01










  • @pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
    – Lord-Link
    Nov 20 '18 at 14:02


















  • are you using windows auth with .net core?
    – Daniel A. White
    Nov 20 '18 at 13:59










  • docs.microsoft.com/en-us/aspnet/core/security/authentication/…
    – Daniel A. White
    Nov 20 '18 at 13:59










  • @DanielA.White Yes I am using windows auth with .net core
    – Lord-Link
    Nov 20 '18 at 14:01










  • @Lord-Link i mean is it properly configured?
    – Daniel A. White
    Nov 20 '18 at 14:01










  • @pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
    – Lord-Link
    Nov 20 '18 at 14:02
















are you using windows auth with .net core?
– Daniel A. White
Nov 20 '18 at 13:59




are you using windows auth with .net core?
– Daniel A. White
Nov 20 '18 at 13:59












docs.microsoft.com/en-us/aspnet/core/security/authentication/…
– Daniel A. White
Nov 20 '18 at 13:59




docs.microsoft.com/en-us/aspnet/core/security/authentication/…
– Daniel A. White
Nov 20 '18 at 13:59












@DanielA.White Yes I am using windows auth with .net core
– Lord-Link
Nov 20 '18 at 14:01




@DanielA.White Yes I am using windows auth with .net core
– Lord-Link
Nov 20 '18 at 14:01












@Lord-Link i mean is it properly configured?
– Daniel A. White
Nov 20 '18 at 14:01




@Lord-Link i mean is it properly configured?
– Daniel A. White
Nov 20 '18 at 14:01












@pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
– Lord-Link
Nov 20 '18 at 14:02




@pstrjds Ya it does get the user but as you mentioned it gets the user name that the application is running under. I want to get the user name of the user accessing the intranet (the client machine). Is this possible and how would I go about this?
– Lord-Link
Nov 20 '18 at 14:02












2 Answers
2






active

oldest

votes


















1














Searching for a solution I ran into this site:



Identities for different IIS7 Authentication Configurations



Looking at the different identity names that are used with the different application pool settings I decided to try changing it. What kinda clued me into this is the user name I was getting back in my code (NT AUTHORITYNETWORK SERVICE) was very similar to the Identity setting NetworkService. So I attempted trying to load the page after changing the application pools identity to LocalSystem and this solved my issue!



Below is a screenshot of the application pool setting that i changed:



Application Pool Identity Setting



Anyhow this solved my problem hope this helps someone who may have the same issue!






share|improve this answer





























    0














    I just created a new ASP.NET Core 2.1 MVC Web Application Project from Template in Visual Studio. Checked "Windows Authentication" in the Template Wizard and



    added User.Identity.Name



     public IActionResult Contact()
    {
    ViewData["Message"] = "User: " + User.Identity.Name;

    return View();
    }


    works out of the box on dev machine.
    You ran into problems only when moving to an production IIS?
    So it will be difficult to narrow down the diffs from remote.






    share|improve this answer





















    • Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
      – Lord-Link
      Nov 20 '18 at 14:41










    • same when you publish it as Web App on Azure with AAD?
      – Falco Alexander
      Nov 20 '18 at 14:57










    • Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
      – Lord-Link
      Nov 20 '18 at 14:59











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394606%2fnet-core-2-1-getting-client-machines-windows-user-name%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Searching for a solution I ran into this site:



    Identities for different IIS7 Authentication Configurations



    Looking at the different identity names that are used with the different application pool settings I decided to try changing it. What kinda clued me into this is the user name I was getting back in my code (NT AUTHORITYNETWORK SERVICE) was very similar to the Identity setting NetworkService. So I attempted trying to load the page after changing the application pools identity to LocalSystem and this solved my issue!



    Below is a screenshot of the application pool setting that i changed:



    Application Pool Identity Setting



    Anyhow this solved my problem hope this helps someone who may have the same issue!






    share|improve this answer


























      1














      Searching for a solution I ran into this site:



      Identities for different IIS7 Authentication Configurations



      Looking at the different identity names that are used with the different application pool settings I decided to try changing it. What kinda clued me into this is the user name I was getting back in my code (NT AUTHORITYNETWORK SERVICE) was very similar to the Identity setting NetworkService. So I attempted trying to load the page after changing the application pools identity to LocalSystem and this solved my issue!



      Below is a screenshot of the application pool setting that i changed:



      Application Pool Identity Setting



      Anyhow this solved my problem hope this helps someone who may have the same issue!






      share|improve this answer
























        1












        1








        1






        Searching for a solution I ran into this site:



        Identities for different IIS7 Authentication Configurations



        Looking at the different identity names that are used with the different application pool settings I decided to try changing it. What kinda clued me into this is the user name I was getting back in my code (NT AUTHORITYNETWORK SERVICE) was very similar to the Identity setting NetworkService. So I attempted trying to load the page after changing the application pools identity to LocalSystem and this solved my issue!



        Below is a screenshot of the application pool setting that i changed:



        Application Pool Identity Setting



        Anyhow this solved my problem hope this helps someone who may have the same issue!






        share|improve this answer












        Searching for a solution I ran into this site:



        Identities for different IIS7 Authentication Configurations



        Looking at the different identity names that are used with the different application pool settings I decided to try changing it. What kinda clued me into this is the user name I was getting back in my code (NT AUTHORITYNETWORK SERVICE) was very similar to the Identity setting NetworkService. So I attempted trying to load the page after changing the application pools identity to LocalSystem and this solved my issue!



        Below is a screenshot of the application pool setting that i changed:



        Application Pool Identity Setting



        Anyhow this solved my problem hope this helps someone who may have the same issue!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 19:06









        Lord-LinkLord-Link

        40621132




        40621132

























            0














            I just created a new ASP.NET Core 2.1 MVC Web Application Project from Template in Visual Studio. Checked "Windows Authentication" in the Template Wizard and



            added User.Identity.Name



             public IActionResult Contact()
            {
            ViewData["Message"] = "User: " + User.Identity.Name;

            return View();
            }


            works out of the box on dev machine.
            You ran into problems only when moving to an production IIS?
            So it will be difficult to narrow down the diffs from remote.






            share|improve this answer





















            • Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
              – Lord-Link
              Nov 20 '18 at 14:41










            • same when you publish it as Web App on Azure with AAD?
              – Falco Alexander
              Nov 20 '18 at 14:57










            • Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
              – Lord-Link
              Nov 20 '18 at 14:59
















            0














            I just created a new ASP.NET Core 2.1 MVC Web Application Project from Template in Visual Studio. Checked "Windows Authentication" in the Template Wizard and



            added User.Identity.Name



             public IActionResult Contact()
            {
            ViewData["Message"] = "User: " + User.Identity.Name;

            return View();
            }


            works out of the box on dev machine.
            You ran into problems only when moving to an production IIS?
            So it will be difficult to narrow down the diffs from remote.






            share|improve this answer





















            • Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
              – Lord-Link
              Nov 20 '18 at 14:41










            • same when you publish it as Web App on Azure with AAD?
              – Falco Alexander
              Nov 20 '18 at 14:57










            • Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
              – Lord-Link
              Nov 20 '18 at 14:59














            0












            0








            0






            I just created a new ASP.NET Core 2.1 MVC Web Application Project from Template in Visual Studio. Checked "Windows Authentication" in the Template Wizard and



            added User.Identity.Name



             public IActionResult Contact()
            {
            ViewData["Message"] = "User: " + User.Identity.Name;

            return View();
            }


            works out of the box on dev machine.
            You ran into problems only when moving to an production IIS?
            So it will be difficult to narrow down the diffs from remote.






            share|improve this answer












            I just created a new ASP.NET Core 2.1 MVC Web Application Project from Template in Visual Studio. Checked "Windows Authentication" in the Template Wizard and



            added User.Identity.Name



             public IActionResult Contact()
            {
            ViewData["Message"] = "User: " + User.Identity.Name;

            return View();
            }


            works out of the box on dev machine.
            You ran into problems only when moving to an production IIS?
            So it will be difficult to narrow down the diffs from remote.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 14:33









            Falco AlexanderFalco Alexander

            1,141818




            1,141818












            • Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
              – Lord-Link
              Nov 20 '18 at 14:41










            • same when you publish it as Web App on Azure with AAD?
              – Falco Alexander
              Nov 20 '18 at 14:57










            • Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
              – Lord-Link
              Nov 20 '18 at 14:59


















            • Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
              – Lord-Link
              Nov 20 '18 at 14:41










            • same when you publish it as Web App on Azure with AAD?
              – Falco Alexander
              Nov 20 '18 at 14:57










            • Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
              – Lord-Link
              Nov 20 '18 at 14:59
















            Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
            – Lord-Link
            Nov 20 '18 at 14:41




            Ya working off the dev machine it worked just fine its as soon as it goes into a production IIS server that it doesn't get the client user.
            – Lord-Link
            Nov 20 '18 at 14:41












            same when you publish it as Web App on Azure with AAD?
            – Falco Alexander
            Nov 20 '18 at 14:57




            same when you publish it as Web App on Azure with AAD?
            – Falco Alexander
            Nov 20 '18 at 14:57












            Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
            – Lord-Link
            Nov 20 '18 at 14:59




            Not sure what the result would be on Azure because we aren't using Azure and I don't have an account to test off for Azure.
            – Lord-Link
            Nov 20 '18 at 14:59


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394606%2fnet-core-2-1-getting-client-machines-windows-user-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?