地理服务器中的身份验证 - asp.net 中的 POST 方法
Posted
技术标签:
【中文标题】地理服务器中的身份验证 - asp.net 中的 POST 方法【英文标题】:Authentication in geoserver - POST method in asp.net 【发布时间】:2012-06-28 22:41:31 【问题描述】:我想使用我的 asp.net 应用程序进行 POST 调用以在 geoserver 中进行身份验证,这是我的代码
HttpWebRequest req = WebRequest.Create(new Uri("http://localhost:1979/geoserver/rest")) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/xml";
string authInfo = "admin:geoserver";
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic"+ authInfo));
// Build a string with all the params, properly encoded.
// We assume that the arrays paramName and paramVal are
// of equal length:
StringBuilder paramz = new StringBuilder();
for (int i = 0; i < paramName.Length; i++)
paramz.append(paramName[i]);
paramz.append("=");
paramz.append(HttpUtility.UrlEncode(paramVal[i]));
paramz.append("&");
paramz.Append("username");
paramz.Append("=");
paramz.Append("admin");
paramz.Append("&");
paramz.Append("password");
paramz.Append("=");
paramz.Append("geoserver");
CredentialCache cc = new CredentialCache();
cc.Add(
new Uri("http://localhost:1979/geoserver/rest"),
"Basic",
new NetworkCredential("admin", "geoserver", "localhost"));
req.Credentials = new NetworkCredential("admin", "geoserver", "localhost");
//req.Credentials = CredentialCache.DefaultCredentials;
//req.Credentials = cc;
//req.AllowAutoRedirect = true;
// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
post.Write(formData, 0, formData.Length);
// Pick up the response:
string result = null;
HttpWebResponse resp=null;
try
resp = (HttpWebResponse)req.GetResponse();
using (resp as HttpWebResponse)
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
catch (WebException we)
HttpWebResponse errorResponse = we.Response as HttpWebResponse;
if (errorResponse.StatusCode == HttpStatusCode.NotFound)
即使我包含我的登录凭据,我也会收到此错误“远程服务器返回错误:(401) 未经授权”
提前致谢
【问题讨论】:
【参考方案1】:改变这一行
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic"+ authInfo));
到
req.Headers["Authorization"] = Convert.ToBase64String(Encoding.ASCII.GetBytes("Basic "+ authInfo));
Authorization 标头实际上是 Basic YyXxZzEtcEtc
与嵌入空间。
【讨论】:
以上是关于地理服务器中的身份验证 - asp.net 中的 POST 方法的主要内容,如果未能解决你的问题,请参考以下文章
从 ASP.NET/Blazor 服务器中的当前经过身份验证的用户获取数据