最近我在一个办公室工作,无线网络使用令人讨厌的身份验证方案:每隔几个小时,您需要打开浏览器并在身份验证网页中输入用户名/密码,否则您将失去网络访问权限。(当时间到期时,您的下一个浏览器请求将重定向到身份验证页面,如果您的凭据通过集合,那么您将被重定向回您最初尝试访问的页面)。
对于机场或咖啡店的无线网络来说,这种烦恼可能没问题,但在办公室里,这很令人恼火——尤其是如果您使用的网络服务(例如 SVN、电子邮件)每隔几个小时就会突然停止工作,除非您提出浏览器。
因此,我编写了一个小型 C# 控制台应用程序,它将通过使用我的凭据向登录表单发送 HTTP 请求来为我登录。
这显然是不安全的——我的密码在我的源代码中供所有人查看。我希望能够使用与 IE 相同的机制来保存我的凭据,例如,用于在 Web 表单中保存和重新填写密码。
理想情况下,我想要一个用于输入、保存和检索凭据的可重用组件(包括带有可选“保存凭据”复选框的 UI),以便我的应用程序可以简单地执行以下操作(在伪代码中):
// retrieve any saved credentials from some secure place
Credentials creds = GetCreds(some parameters go here);
// if none stored, then show the user an "enter and optionally save credentials" dialog
if (creds == null)
creds = GetCredsDialog(some parameters go here);
// POST to the authentication page
if (creds != null)
{
string authUrl = "https://somehost/login/";
string postDataPattern = "post data pattern here";
// use SecureString here instead?
string postData = string.Format (postDataPattern, HttpUtility.HtmlEncode(creds.Username), HttpUtility.HtmlEncode(creds.Password));
WebClient wc = new WebClient();
string html = wc.UploadString (authUrl, "POST", postData);
// TODO: if html indicates login failure, clear stored credentials
// and ask for new creds. then retry.
}
本质上,我想将安全存储凭据的负担从我的应用程序转移到 Windows,假设 Windows 人员在这方面会比我做得更好。:-)
我在这里寻找的不是铁的安全性,只是类似于 IE 用来保护我为其他网站存储的其他密码的东西。我只是不想在我的代码中保留纯文本密码!
当然,这里正确的解决方案是与 IT 部门合作,让他们获得真正的无线身份验证方案,但与此同时,我只能靠我自己。
.NET 解决方案更可取,但 Win32 解决方案也可以——我可以简单地将应用程序移植到 C++,而不会遇到太多麻烦。