我正在为 ActiveX HTTP 控件制作一个测试装置,并且需要创建一个可以安全发布的网站。为了简单起见,我使用 VS 调试服务器运行 Web 应用程序; Web 应用程序项目是测试应用程序解决方案的一部分。 AX 控件不支持 NTLM 身份验证。有没有简单的方法可以要求非常基本的 http 身份验证,而无需 NTLM 或重定向到页面表单?我只需要它在发帖过程中需要用户名和密码。用户名和密码内容并不重要,因此所有内容都将以明文形式存储。
我已经尝试过Web.Config中的身份验证模式; Windows 看起来与 NTLM 相同(可能有问题),Forms 需要一个表单来连接并设置 cookie,Passport 超出了该项目的范围,而且我不知道如何实现 None。有任何想法吗?
我在 Web.Config 中尝试了“身份验证模式=“Windows””,并选中了 Web 应用程序的“Web”选项卡中的 NTLM 复选框。
您可以使用 ASP.NET 实现您自己的基本 HTTP 身份验证。它似乎没有就像一个非常复杂的规范,但是请参阅RFC1945了解所有详细信息。
如果我必须这样做,我会从HttpModule
对每个请求运行并检查 HTTP 标头HTTP_AUTHORIZATION
。如果它是基本身份验证的标头,那么您可以解码用户名和密码。如果标头丢失或用户名和密码不正确,则发回 HTTP 401 响应并添加WWW-Authenticate
header.
像这样的东西(未经测试,但你明白了):
public class BasicAuthenticationModule: IHttpModule
{
public void Init(HttpApplication application)
{
application.AuthenticateRequest += new EventHandler(Do_Authentication);
}
private void Do_Authentication(object sender, EventArgs e)
{
var request = HttpContext.Current.Request;
string header = request.Headers["HTTP_AUTHORIZATION"];
if(header != null && header.StartsWith("Basic "))
{
// Header is good, let's check username and password
string username = DecodeFromHeader(header, "username");
string password = DecodeFromHeader(header, password);
if(Validate(username, password)
{
// Create a custom IPrincipal object to carry the user's identity
HttpContext.Current.User = new BasicPrincipal(username);
}
else
{
Protect();
}
}
else
{
Protect();
}
}
private void Protect()
{
response.StatusCode = 401;
response.Headers.Add("WWW-Authenticate", "Basic realm=\"Test\"");
response.Write("You must authenticate");
response.End();
}
private void DecodeFromHeader()
{
// Figure this out based on spec
// It's basically base 64 decode and split on the :
throw new NotImplementedException();
}
private bool Validate(string username, string password)
{
return (username == "foo" && pasword == "bar");
}
public void Dispose() {}
public class BasicPrincipal : IPrincipal
{
// Implement simple class to hold the user's identity
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)