获取当前用户的 NetworkCredential (C#) [英] Getting NetworkCredential for current user (C#)

查看:26
本文介绍了获取当前用户的 NetworkCredential (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从控制台应用程序调用网络服务,我需要为客户端提供一个 System.Net.NetworkCredential 对象.
是否可以在不提示输入用户名/密码的情况下为启动应用程序的用户创建 NetworkCredential 对象?

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 to create a NetworkCredential object for the user that started the application without prompting for username/password?

推荐答案

如果被调用的 Web 服务使用 Windows 集成安全,则应从当前的 WindowsIdentity 创建一个 NetworkCredential足以让 web 服务使用当前用户的 windows 登录.但是,如果 Web 服务使用不同的安全模型,则没有任何方法可以从当前身份中提取用户密码……这本身就是不安全的,允许您(开发人员)窃取您的用户密码.您可能需要为您的用户提供某种方式来提供他们的密码,如果您不希望他们重复提供密码,请将其保存在某个安全的缓存中.

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.

要获取当前身份的凭据,请使用以下内容:

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");

这篇关于获取当前用户的 NetworkCredential (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆