c# - 从 Windows 身份验证中获取 `ICredentials`

标签 c# asp.net .net asp.net-mvc authentication

我正在使用 Windows 身份验证。

我需要登录服务。它需要使用 ICredentials 对象进行身份验证,但宁愿避免要求用户再次输入其凭据。

有没有办法从当前登录的用户那里获取ICredentials

该服务是 TFS SDK。

最佳答案

MVC 中的 Controller 有一个属性 User,它是 IPrincipal

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.user%28v=vs.108%29.aspx

IPrincipal 有一个 Identity 属性

http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.identity%28v=vs.110%29.aspx

属于IIdentity

类型

http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity%28v=vs.110%29.aspx

如果您使用的是 Windows 身份验证,则实际实例的类型为 WindowsIdentity

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity%28v=vs.110%29.aspx

希望它包含您需要的所有凭据信息。

此外,您可能想尝试:

Uri uri = new Uri("http://tempuri.org/");
ICredentials credentials = CredentialCache.DefaultNetworkCredentials;
NetworkCredential credential = credentials.GetCredential(uri, "Basic");

关于c# - 从 Windows 身份验证中获取 `ICredentials`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070888/

相关文章:

c# - 将 Azure Blob 容器中的 file.bacpac 导入到 Azure SQL 数据库

c# - 如何仅 Ping IPv4?

c# - 如何使用 Lucene 对 Sitecore 中具有多个位置的文档进行地理空间搜索?

asp.net - Azure启动任务中的aspnet_compiler

c# - 如何访问正在运行的 .net 应用程序

.net 正则表达式超过 2 个连续字母

.net - Wpf - 以主桌面为中心

c# - 绑定(bind) EasingColorKeyframe 值

c# - 是否可以动态更改 MS - SimpleMembership 数据库连接?

c# - SignalR 2.0 错误 : Could not load file or assembly Microsoft. Owin.Security