-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to run .NET 5.0 service as a different user #51269
Comments
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsDescriptionI am trying to get an ASP.NET 5.0 service to run as a different user (by using I am aware that this is a very general error about a process exiting, therefore, I generated a small sample to reproduce the bug locally (see code below). public static void Main(string[] args)
{
//This is here so that one can attach to the process
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureServices(services =>
{
});
try
{
hostBuilder.UseWindowsService();//Line where error occurs
}
catch (Exception e)
{
Console.WriteLine(e);
}
return hostBuilder;
} Steps to reproduce the bug:
Expected behavior: Since I am using Configuration
Regression?I don't know. Other information
I believe the error lies within the Do you know of any workarounds?: I know that I can install the service then go to the properties of that service and set the logon credentials as the specified user, as a workaround. The service then starts up and is seen as
|
I found I was able to work around this issue with dotnet /path/to/Historian.dll So maybe something to do with the apphost.exe? |
The stack trace above is coming from I wonder if that change would fix this issue as well. Moving to 7.0 and marking as "up for grabs". |
After doing a little bit of testing it appears that the cause of the issue is not the failed validation of At this point a work around to avoid the exception when directly executing the
In this case parent would be null, the exe would not execute as windows service (because we are doing it directly) but the exception will be avoided. So, the deletion of the check may be to enough to solve the issue. |
Does this issue still remain after closing #52416 ? |
@KieranFoot sure since it is up for grabs, it would be good to investigate and propose a fix if this is still a problem after the lastest changes |
Is this .NET 5.0 (EOL) specific or does this bug also occur on .NET 6/7? |
we think it is fixed in 7.0 @deeprobin or @KieranFoot would you be able to test if this with the latest to confirm repro? |
Hi there, Just to give a bit of an update to the original bug.
Our current workaround is still the same as the original code sample attached: public static void Main(string[] args)
{
//This is here so that one can attach to the process
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.ConfigureServices(services =>
{
});
try
{
hostBuilder.UseWindowsService();//Line where error occurs
}
catch (Exception e)
{
Console.WriteLine(e);
}
return hostBuilder;
} We are still using this workaround in .NET 6. When we have the capacity to test if the desired workflow works on our deployment machines we will and then reply on this thread. We ideally would like this to be resolved in .NET 6. Thanks everybody for the feedback and input. |
I believe this issue has been fixed in .NET 7. We are no longer calling "Process.get_SessionId()" which is in the callstack in the original issue. Please re-open if this issue still reproduces using the latest 7.0 build. |
Thank you @eerhardt |
Description
I am trying to get an ASP.NET 5.0 service to run as a different user (by using
shift
+right click
and selecting Run as different user) because of a DCOM requirement, but I am unable to do so and simply receive an error (seen within Other information section below).I am aware that this is a very general error about a process exiting, therefore, I generated a small sample to reproduce the bug locally (see code below).
Steps to reproduce the bug:
Create a new local user account in windows
Give the created user account full control of the containing debug folder (right click > properties > security > edit > add new user > assign "Full Control" > save)
Give the created user account "Logon As Service" rights (Control Panel > Administrative Tools > Local Security Policy > Dropdown "Local Policies" > Double Click "User Rights Assignment" > Double Click "Logon as a service" > Add your created user account > OK)
Shift + Right Click > Run as different user on the test.exe console app (see code below)
Enter local user name and password and run
Attach to the process from Visual Studio (
Console.ReadLine();
gives you time to attach to process)Step through code and see error on "Line where error occurs"
Expected behavior:
Since I am using
UseWindowsService()
, I expect the app to start up with the provided user credentials.Configuration
Regression?
I don't know.
Other information
I believe the error lies within the
hostBuilder.UseWindowsService();//Line where error occurs
line, seen in the Description section above.Do you know of any workarounds?:
I know that I can install the service then go to the properties of that service and set the logon credentials as the specified user, as a workaround. The service then starts up and is seen as
running
, however, the expected output from the service is not received. Therefore, I suspect that therunning
status is a false positive and the service is actually in an errored state or the service is still not using the logon credentials correctly.The text was updated successfully, but these errors were encountered: