简体   繁体   English

如何使用C#在Windows上存储和检索凭据

[英]How to store and retrieve credentials on Windows using C#

I build a C# program, to be run on Windows 10. I want to send emails from this program (calculation results) by just pressing a button. 我构建了一个C#程序,在Windows 10上运行。我想通过按下按钮从该程序发送电子邮件(计算结果)。 I put the from: e-mail address and the subject: , etc. in C# properties, but I do not want to put a clear text password anywhere in the program, AND I don't want the user to have to type in the password for the server each time a mail is sent. 我把from:电子邮件地址和subject:等等放在C#属性中,但是我不想在程序的任何地方放置明文密码,而且我不希望用户必须输入每次发送邮件时服务器的密码。

Can that be done? 可以这样做吗?

If so, how (generally)? 如果是这样,如何(一般)?

I was thinking of putting all that e-mail information, including an encrypted password for the server in a data file to be read during startup of the program. 我正在考虑将所有电子邮件信息(包括服务器的加密密码)放在数据文件中,以便在程序启动时读取。

Or maybe Windows 10 has a facility for that... 或者也许Windows 10有一个设施...

You can use the Windows Credential Management API. 您可以使用Windows凭据管理API。 This way you will ask the user for the password only once and then store the password in Windows Credentials Manager. 这样,您只需要用户输入一次密码,然后将密码存储在Windows凭据管理器中。

Next time your application starts and it needs to use the password it will read it from Windows Credentials Manager. 下次启动应用程序并需要使用密码时,它将从Windows凭据管理器中读取它。 One can use the Windows Credential Management API directly using P/Invoke ( credwrite , CredRead , example here ) or via a C# wrapper CredentialManagement . 可以使用P / InvokecredwriteCredRead此处示例 )或通过C#包装器CredentialManagement直接使用Windows Credential Management API。


Sample usage using the NuGet CredentialManagement package: 使用NuGet CredentialManagement包的示例用法:

public class PasswordRepository
{
    private const string PasswordName = "ServerPassword";

    public void SavePassword(string password)
    {
        using (var cred = new Credential())
        {
            cred.Password = password;
            cred.Target = PasswordName;
            cred.Type = CredentialType.Generic;
            cred.PersistanceType = PersistanceType.LocalComputer;
            cred.Save();
        }
    }

    public string GetPassword()
    {
        using (var cred = new Credential())
        {
            cred.Target = PasswordName;
            cred.Load();
            return cred.Password;
        }
    }
}

I don't recommend storing passwords in files on client machines. 我不建议将密码存储在客户端计算机上的文件中。 Even if you encrypt the password, you will probably embed the decryption key in the application code which is not a good idea. 即使您加密密码,您也可能将解密密钥嵌入应用程序代码中,这不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 C# 从 Windows Credentials Store 检索凭据 - Retrieve credentials from Windows Credentials Store using C# 在IIS下的ASP.net网站中使用C#从Windows通用凭据存储中检索凭据 - Retrieve Credentials from Windows generic Credentials Store using C# in ASP.net website under IIS 在 IIS 下的 ASP.net 网站中使用 C# 从 Windows Credentials Store 检索凭据 - Retrieve Credentials from Windows Credentials Store using C# in ASP.net website under IIS 如何使用C#检索Windows Store应用程序中的所有联系人 - How to retrieve all contacts in Windows Store app using c# 如何检索实时媒体流的HttpResponseMessage内容标头(在Windows商店应用中,使用C#) - How to retrieve HttpResponseMessage content headers for a live media stream (in a windows store app using c#) 如何使用Windows登录凭据进行代理身份验证使用C# - how to use the windows login credentials for proxy authentication using C# C#-使用该Windows凭据对话框登录 - C# - Log In By Using That Windows Credentials Dialog 如何存储和检索使用C#加密的数据库连接密码 - How to store and retrieve database connection password encrypted using C# 如何使用.Net(C#/ VB)在MySQL中存储/检索HTML - How to store/retrieve HTML in MySQL using .Net (C#/VB) 如何:使用 C# 将 pdf 文件存储和检索到 mysql - How To: Store and Retrieve pdf file to mysql using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM