How to add Windows Credentials to Credentials Manager on Windows programmatically?

巧了我就是萌 提交于 2020-01-02 08:04:34

问题


I have looked at this question's selected answer Retrieve Credentials from Windows Credentials Store using C# , which uses the CredentialManagement NuGet package to get and set credentials on Windows.

Credential Management package is a wrapper for the Windows Credential Management API that supports both the old and the new style of UI

I was able to set new credentials that way, however they were set as Generic Credentials.

public static bool SetCredentials(
     string target, string username, string password, PersistanceType persistenceType)
    {
        return new Credential
        {
            Target = target,
            Username = username,
            Password = password,
            PersistanceType = persistenceType
        }.Save();
    }

According to this, there are at least 4 different types of credentials that the Windows Credentials Manager can use:

  • Windows Credentials
  • Certificate-Based Credentials
  • Generic Credentials
  • Web Credentials

The credentials I need to set are intended to allow access to a specific local web server, and when I have added them manually as Windows Credentials they work, when they get added as Generic Credentials by the application, or myself, they don't work.

I couldn't find enough information about this here, so what I need to know is how to add Windows Credentials to the Windows Credentials Manager by using this package, or in any other way that can be done programmatically.

UPDATE: I managed to solve my issue by doing it the way it's shown on this question: C# Using CredWrite to Access C$. However, since I didn't want to answer the same thing here as well, I will leave the question open in case someone knows how to do it differently, or else I will vote to close it.

UPDATE 2: Although by the time the previous update was posted the solution that was found then was working, it's not working now. So I'm still looking for a way to completely solve this problem. I'm sure the previous link can still be useful for someone else though.

来源:https://stackoverflow.com/questions/49033193/how-to-add-windows-credentials-to-credentials-manager-on-windows-programmaticall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!