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

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

问题描述

我试图调用从控制台应用程序Web服务,我需要提供一个 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服务使用集成的安全性窗口,创建一个的NetworkCredential 从目前的的WindowsIdentity 应该足以允许Web服务使用登录当前用户的窗口。但是,如果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天全站免登陆