Getting NetworkCredential for current user (C#)

后端 未结 2 463
天命终不由人
天命终不由人 2020-11-30 03:38

I\'m trying to invoke a webservice from a console application, and I need to provide the client with a System.Net.NetworkCredential object.
Is it possible t

相关标签:
2条回答
  • 2020-11-30 04:19

    If the web service being invoked uses windows integrated security, creating a NetworkCredential from the current WindowsIdentity should be sufficient to allow the web service to use the current users windows login. However, if the web service uses a different security model, there isn't any way to extract a users password from the current identity ... that in and of itself would be insecure, allowing you, the developer, to steal your users passwords. You will likely need to provide some way for your user to provide their password, and keep it in some secure cache if you don't want them to have to repeatedly provide it.

    Edit: To get the credentials for the current identity, use the following:

    Uri uri = new Uri("http://tempuri.org/");
    ICredentials credentials = CredentialCache.DefaultCredentials;
    NetworkCredential credential = credentials.GetCredential(uri, "Basic");
    
    0 讨论(0)
  • 2020-11-30 04:21

    You can get the user name using System.Security.Principal.WindowsIdentity.GetCurrent() but there is not way to get current user password!

    0 讨论(0)
提交回复
热议问题