使用Windows服务进行Git身份验证

时间:2020-07-14 23:29:01

标签: c# windows git authentication windows-services

我有一个用C#编写的Windows服务。我想将服务帐户设置为LocalSystem。该服务产生一个进程,该进程是一个批处理文件,在其中需要进行Git身份验证。如果我将服务帐户设置为“用户”,则安装时会提示输入用户名和密码,并且服务运行良好。但是,当我将其注册为LocalSystem帐户时,它没有这样做。 事件查看器错误日志为:

Source: Git Credential Manager
Details: System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

我想使用LocalSystem帐户的原因是我想避免任何凭据弹出窗口。关于如何安装服务而不弹出任何密码,验证git以及当前登录用户的任何帮助?

1 个答案:

答案 0 :(得分:0)

如果要避免提示输入凭据,则需要正确设置凭据助手。您可能确实在使用一个,在这种情况下为Windows的Git Credential Manager,但在这种情况下需要使用UI提示。

您首先需要通过输入git config --system --unset credential.helper确保系统Git安装未使用凭据帮助程序。然后,将服务配置为在调用push或fetch命令时使用适当的credential.helper参数,例如:git -c credential.helper=my-credential-helper push origin main

您可以按照gitcredentials(7)手册页中的说明编写自己的凭据帮助程序,并指定该名称,也可以使用shell命令从环境中获取它。例如,如果要推送代码并从USERNAMEPASSWORD环境变量读取数据,则可以运行以下命令:

$ git -c credential.helper='f() { echo username=$USERNAME; echo password=$PASSWORD; };f' \
    push origin main