首页 > 解决方案 > 如何使用 Windows 登录凭据在 Web 浏览器中自动登录

问题描述

我有一个网站(内部应用程序),它需要用户名和密码,这与他们的 Windows 登录凭据相同。单点登录在技术上是不可能的。我有大约 1000 个客户每次都需要输入他们的帐户详细信息,所以我想自动执行此操作。是否可以编写用户单击桌面上的快捷方式并使用网站上的 Windows 凭据自动登录的脚本?感谢您的任何想法。

标签: windowsgoogle-chromeinternet-explorer

解决方案


如果您的站点托管在 IIS 服务器上供内部使用,那么您可以尝试参考下面的示例,这可能会帮助您解决问题。

代码:

// Ensure Directory Security settings for default web site in IIS is "Windows Authentication".
string url = "http://localhost";
// Create a 'HttpWebRequest' object with the specified url. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
// Assign the credentials of the logged in user or the user being impersonated.
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
// Send the 'HttpWebRequest' and wait for response.            
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
Console.WriteLine("Authentication successful");
Console.WriteLine("Response received successfully");

DefaultCredentials 表示当前运行应用程序的安全上下文的系统凭据。对于客户端应用程序,这些通常是运行应用程序的用户的 Windows 凭据(用户名、密码和域)。对于 ASP.NET 应用程序,默认凭据是登录用户或被模拟用户的用户凭据。

参考:

CredentialCache.DefaultCredentials 属性


推荐阅读