问题描述
在我的C#项目中,有带有API键的constring的Apikeys.cs文件. 我希望这些字符串在GIT服务器中为空,但我本地计算机中有实际的API键. 因此,拉动项目的人可以毫无问题地编译它,而我的本地计算机仍将在同一文件中包含API键.
如果我尝试使用空字符串上传Apikeys.cs文件,那么我将无法使用API键的本地文件,因为当我尝试按下它时,它将覆盖空apikeys.cs文件.另外,我也不能忽略此文件,因为它会从git服务器中删除空apikeys.cs文件.
那么,此问题的最佳自动化方法是什么,该方法将允许服务器中的类文件中的类文件,因此当人们将其拉动并在本地计算机中使用真实类文件时,项目将是可以编译的?
推荐答案
我现在想到了另一种解决方案,这还不是完美,但对我来说仍然足够好,例如:
APIKeys.cs文件:
public static partial class APIKeys { public static readonly string ImgurClientID = ""; public static readonly string ImgurClientSecret = ""; public static readonly string GoogleClientID = ""; public static readonly string GoogleClientSecret = ""; public static readonly string PastebinKey = ""; ... }
APIKeysLocal.cs文件:
public static partial class APIKeys { static APIKeys() { ImgurClientID = "1234567890"; ImgurClientSecret = "1234567890"; GoogleClientID = "1234567890"; GoogleClientSecret = "1234567890"; PastebinKey = "1234567890"; ... } }
git中的APIKeysLocal.cs git中的文件和没有此文件的人如果从解决方案资源管理器中删除此文件,仍然可以编译项目.
我还使用项目pre Build Event尚不存在:
自动创建空APIKeysLocal.cs文件.cd $(ProjectDir)APIKeys\ if not exist APIKeysLocal.cs ( type nul > APIKeysLocal.cs )
这样,用户无需做任何事情就能编译项目.
其他推荐答案
接受您不能将未加密的私钥隐藏在公共空间中.
您可以做的是将密钥移至私人空间,然后从代码中引用该私人空间.
您的私人空间可能是环境变量或Windows注册表,它应该是应用程序源代码之外的内容.
另一种方法是创建专门存储私钥的新配置文件(例如keys.config),然后将此文件从源控件中排除.
这意味着您不共享私钥,但这也意味着您需要记录用户需要重新创建自己的keys.config的(也许是在readme.md中).更好(感谢 @joey )是在解决方案中包括一个示例配置文件(keys.sample.config),说明所需的内容.
其他推荐答案
您有两个选择:
-
告诉git忽略本地计算机上APIKeys.cs的更改:
git update-index --skip-worktree APIKeys.cs
这将导致本地更改不承诺.如果您 do 想对文件提交更改,则必须使用--no-skip-worktree flag撤消此文件.
-
将APIKeys.cs文件重命名为APIKeys.template.cs之类的东西,其中包含要共享的空白字符串.将此文件保存在您的存储库中.将该文件复制到APIKeys.cs.将APIKeys.cs添加到您的.gitignore中.添加指令复制模板文件并使用本地设置进行修改.
git mv APIKeys.cs APIKeys.template.cs $EDITOR APIKeys.template.cs git commit cat APIKeys.cs >> .gitignore cp APIKeys.template.cs APIKeys.cs
问题描述
In my C# project have APIKeys.cs file which have const strings with API keys. I want those strings to be empty in Git server but have actual API keys in my local computer. So peoples who pull project can compile it without problem and still my local computer gonna have API keys in same file.
If I try to upload APIKeys.cs file with empty strings then I can't have local file with API keys because when I try to push it, it will overwrite empty APIKeys.cs file. Also I can't ignore this file too because it will remove empty APIKeys.cs file from Git server.
So what is best automated approach for this problem which will allow class file with empty strings in server, so project will be compileable when people pull it and have real class file in local computer?
推荐答案
I figured another solution now which is not perfect but still good enough for me, example:
APIKeys.cs file:
public static partial class APIKeys { public static readonly string ImgurClientID = ""; public static readonly string ImgurClientSecret = ""; public static readonly string GoogleClientID = ""; public static readonly string GoogleClientSecret = ""; public static readonly string PastebinKey = ""; ... }
APIKeysLocal.cs file:
public static partial class APIKeys { static APIKeys() { ImgurClientID = "1234567890"; ImgurClientSecret = "1234567890"; GoogleClientID = "1234567890"; GoogleClientSecret = "1234567890"; PastebinKey = "1234567890"; ... } }
Ignore APIKeysLocal.cs file in Git and people who don't have this file can still be able to compile project if they remove this file from solution explorer.
I also automatically create empty APIKeysLocal.cs file if it is not already exists using project pre build event:
cd $(ProjectDir)APIKeys\ if not exist APIKeysLocal.cs ( type nul > APIKeysLocal.cs )
That way user don't need to do anything to be able to compile project.
其他推荐答案
Accept that you cannot hide unencrypted private keys in a public space.
What you can do is move the keys to a private space, and then reference that private space from code.
Your private space might be environment variables or the Windows registry, it should be something outside the source code of your app.
Another approach is to create a new config file (e.g. keys.config) specifically for storing private keys, and then exclude this file from source control.
This means you don't share your private keys, but it also means that you need to document (perhaps in readme.md) that users will need to recreate their own keys.config. Even better (thanks @Joey) is to include a sample config file (keys.sample.config) in the solution, illustrating what's needed.
其他推荐答案
You've got two options:
Tell Git to ignore changes to APIKeys.cs on your local machine:
git update-index --skip-worktree APIKeys.cs
This will cause local changes not to get committed. If you ever do want to commit changes to the file, you'll have to undo this with the --no-skip-worktree flag.
Rename the APIKeys.cs file to something like APIKeys.template.cs, containing the blank strings that you want to share. Keep this file in your repository. Copy that file to APIKeys.cs. Add APIKeys.cs to your .gitignore. Add instructions to copy the template file and modify with local settings.
git mv APIKeys.cs APIKeys.template.cs $EDITOR APIKeys.template.cs git commit cat APIKeys.cs >> .gitignore cp APIKeys.template.cs APIKeys.cs