0

Every where they are giving answers for getting user......but i want to get credentials from Credential Manager? I have tried all the ways using .,

1.System.Security.Principal.WindowsIdentity.GetCurrent().Name

**Output:** => "DESKTOP\\India"

This is giving domain name and not credentail manager credentials. 2.I have tried below code..using credential package from nuget

var cm = new Credential();

cm.Target = "Here what i have to put????";

I got a doubt here to what i have to put in cm.target="?????" 3.Tried

Environment.UserName()

Output: "India"(This is not from credential manager credentials)

4.Tried

System.Net.CredentialCache.DefaultCredentials()

Output:
{

`Username:'',`
password:''
}

Here i think we have to send the parametred and these are also not from windows credential manager credentials.

Iam waiting for help from you folks.any help will be appreciated.

sai
  • 239
  • 1
  • 3
  • 7
  • Could you rephrase your question a bit, it's hard to understand what you mean. This may help: https://stackoverflow.com/help/how-to-ask – papanito Oct 18 '19 at 09:11
  • How to get Windows Credentials (Username and password) using c#.Net – sai Oct 18 '19 at 09:19
  • I wonder if that's even supposed to be supported as passwords should be hashed, meaning they would have to be de-hashed too. OP just wants to know how to retrieve a username and password of the authenticated user currently logged in Windows running the app. @papanito – Barrosy Oct 18 '19 at 09:30
  • Possible duplicate: [Get windows user password in a Windows Forms application](https://stackoverflow.com/questions/50527810/get-windows-user-password-in-a-windows-forms-application) – Barrosy Oct 18 '19 at 09:37
  • i need username from credential manager ....no need for password – sai Oct 18 '19 at 09:49
  • Possible duplicate: [How do I get the current username in .NET using C#?](https://stackoverflow.com/questions/1240373/how-do-i-get-the-current-username-in-net-using-c) – Barrosy Oct 18 '19 at 10:03
  • No it is not duplicate, in your link its not getting windows credentials. its getting desktop name there – sai Oct 18 '19 at 13:42

2 Answers2

2

In short, Windows credentials under the credential manager can only obtain usernames and not passwords. This is determined by the Native underlying API. As for how to get the usernames in Windows Credential, please refer to the below link.
https://github.com/AdysTech/CredentialManager
The usage (the target parameter means the target network/internet address, namely the name in the left side):

//get the username of "a" credential. username:b, password:123456
var result=CredentialManager.GetCredentials("a", CredentialManager.CredentialType.Windows);
            Console.WriteLine(result.UserName+": "+result.Password);

enter image description here
Result.
enter image description here
Here is a discussion related to this subject.
Retrieve credentials from Windows Credentials Store using C#
wish it is useful to you.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
0

The original answer is confusing. It seems to say only usernames can be retrieved then links a project that can retrieve full credentials. I know this is tagged ASP, but it's also tagged C#. I've had minimal experience with ASP, but the code-behind shouldn't work any differently (and the answer above says nothing about it).

I've been retrieving stored credentials for some time now with PowerShell. Just now, I needed a C# solution (and I can't use that PS package with my project).

https://github.com/meziantou/Meziantou.Framework

Nothing special, but I wrote this now to confirm it worked. StoredCredentialName is the "Internet or network address" field for a generic credential.

    Private Function GetCredentialFromStorage() As Net.NetworkCredential
        Try
            Dim c = Meziantou.Framework.Win32.CredentialManager.ReadCredential(StoredCredentialName)
            Return New Net.NetworkCredential(c.UserName, c.Password)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
Tyler Montney
  • 1,402
  • 1
  • 17
  • 25