开发者

C# WinForm - Save password locally in Credential Manager (Like Keychain does for iOS)

开发者 https://www.devze.com 2023-02-25 13:57 出处:网络
I would like to create a C# WinForms application which will create web requests to servers that require username and password (Basic authentication).开发者_开发知识库

I would like to create a C# WinForms application which will create web requests to servers that require username and password (Basic authentication).

开发者_开发知识库

I want to save the password locally (encrypted of course) on the machine that runs the application.

Basically, I need something that operates as Keychain in iOS, or "secure storage" in eclipse, only for .NET

I've found the Credential Manager in Windows, but all API examples are from .NET Framework V2.0

Isn't there a normal API from Microsoft about how to use it in C#?


Windows applications can use DPAPI for that, the Data Protection API, which stores secrets on a computer, encrypted (indirectly) with a user's login credentials. Any process that runs with the user's privileges can access this data. Optionally you can let the user add his own password (for this one item / secret) if you like (PrompStruct). The DPAPI is also accessible via C# (there are examples on the net).


You can store the credentials in a section in app.config and then encrypt the section (similar to what you'll do in web.config for a web application).

You can use SectionInformation.ProtectSection to protect the section, and SectionInformation.GetRawXml to retrieve the encrypted info (decryption is done transparently).

Example (taken from the MSDN article below):

static public void ProtectSection()
{

    // Get the current configuration file.
    System.Configuration.Configuration config =
            ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.None);


    // Get the section.
    UrlsSection section =
        (UrlsSection)config.GetSection("MyUrls");


    // Protect (encrypt)the section.
    section.SectionInformation.ProtectSection(
        "RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    section.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system
    // uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
        section.SectionInformation.GetRawXml();

    Console.WriteLine("Decrypted section:");
    Console.WriteLine(sectionXml);

}

http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.protectsection.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消