我正在使用 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
属性
属于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/