我正在尝试在我的.NET HttpListener上配置基本访问授权,但我总是遇到相同的错误。我已经尝试了所有可以在这个网站和其他网站上找到的解决方案,但都没有成功。
我需要使用admin/admin作为基本身份验证的用户名/密码。wikipedi page显示了头部应该是什么样子,我遵循了这一点。
我一直收到错误消息“头WWW-Authenticate必须用正确的方法更改,参数:名称”,但是没有必须添加的名为"name“的参数,就像维基百科页面上显示的那样。不幸的是,我已经没有选择了,希望有人能帮上忙。
我的代码如下
private void WebRequestCallback(IAsyncResult result)
{
if (httpListener == null)
{
return;
}
HttpListenerContext context = httpListener.EndGetContext(result);
if (basicAccessAuth)
{
HttpListenerRequest Request = context.Request;
HttpListenerResponse Response = context.Response;
httpListener.AuthenticationSchemes = AuthenticationSchemes.Basic;
NameValueCollection nvCol = new NameValueCollection();
nvCol.Add("Authorization", "admin:admin");
httpListener.Realm = "Overflow";
Request.Headers.Add(nvCol); // error gets thrown here, missing "name" parameter
Response.Headers.Add("WWW-Authenticate: Basic YWRtaW46YWRtaW4=");
HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
MessageBox.Show(identity.Name);
}
httpListener.BeginGetContext(new AsyncCallback(WebRequestCallback), httpListener);
if (ReceiveWebRequest != null)
{
ReceiveWebRequest(context);
}
ProcessRequest(context);
}发布于 2021-03-10 21:35:14
我已经解决了我的问题。我加错了头,应该是Response.AddHeader而不是Response.Headers.Add
https://stackoverflow.com/questions/66547625
复制相似问题