![](/img/trans.png)
[英]Active Directory authentication using ASP.NET MVC 3 and c#
[英]A simple reverse proxy using ASP.NET, C# with authentication
我试图将自定义参数转发到RESTful API服务器并将代理响应返回到面向客户端的服务器。 我不希望客户端有权访问或能够读取API HTTP请求/响应交互,因此我决定使用反向代理执行此操作。 我没有问题转发请求并返回响应。 问题在于身份验证。 面向客户端的服务器总是希望重定向到登录页面,因为它不相信客户端已通过身份验证。 我尝试过使用HTTPS和HTTP,结果相似。
我已经研究了这个问题已经有一段时间了,并且发现了各种各样的答案,其中没有一个似乎完全涵盖了我的具体用例。 我跟随这个例子 ,这是我最接近我特别需要的。 但是,作者注释掉的凭证部分( //request.Credentials = CredentialCache.DefaultCredentials;
)似乎不包括我试图实现的身份验证部分。 请帮我理解这个问题和解决方案。
这是我在控制器中使用的代码:
public ActionResult ProxyEndpoint(string custom_string, string another_custom_string)
{
//Bunch of code here to grab the remoteUrl from AppConfig and do stuff to the parameters and store them in queryString, unnecessary to show here.
//Here's the important bits:
remoteUrl = remoteUrl + "?" + queryString; // create my remoteUrl
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteUrl);
request.Credentials = CredentialCache.DefaultCredentials;
// Also tried this to no avail:
request.Credentials = CredentialCache.DefaultNetworkCredentials;
return ProxyActionResult(request.GetResponse());
}
这是ProxyActionResult
类:
public class ProxyActionResult : ActionResult
{
WebResponse _response;
public ProxyActionResult(WebResponse response)
{
_response = response;
}
public override void ExecuteResult(ControllerContext controllerContext)
{
HttpContextBase httpContext = controllerContext.HttpContext;
WebResponse response = _response;
// Read the byte stream from the response:
Stream responseStream = response.GetResponseStream();
// Pulled this next piece from http://www.codeproject.com/Articles/7135/Simple-HTTP-Reverse-Proxy-with-ASP-NET-and-IIS
// Seemed to fit our use case.
if ((response.ContentType.ToLower().IndexOf("html") >= 0) || (response.ContentType.ToLower().IndexOf("javascript") >= 0))// || (response.ContentType.ToLower().IndexOf("image") >= 0))
{
//If the response is HTML Content, parse it like HTML:
StreamReader readStream = new StreamReader(responseStream, Encoding.Default);
String content;
content = ParseHtmlResponse(readStream.ReadToEnd(), httpContext.Request.ApplicationPath);
//Write the updated HTML to the client(and then close the response):
httpContext.Response.Write(content);
httpContext.Response.ContentType = response.ContentType;
response.Close();
httpContext.Response.End();
}
else
{
// If the response is not HTML Content, write the stream directly to the client:
var buffer = new byte[1024];
int bytes = 0;
while ((bytes = responseStream.Read(buffer, 0, 1024)) > 0)
{
httpContext.Response.OutputStream.Write(buffer, 0, bytes);
}
// from http://www.dotnetperls.com/response-binarywrite
httpContext.Response.ContentType = response.ContentType; // Set the appropriate content type of the response stream.
// and close the stream:
response.Close();
httpContext.Response.End();
}
//throw new NotImplementedException();
}
// Debating whether we need this:
public string ParseHtmlResponse(string html, string appPath)
{
html = html.Replace("\"/", "\"" + appPath + "/");
html = html.Replace("'/", "'" + appPath + "/");
html = html.Replace("=/", "=" + appPath + "/");
return html;
}
事实证明,反向代理代码没有任何问题。 远程服务器是ArcGIS OpenLayers API,它有一个设置,表示crossOrigin: anonymous
。 我评论了这个设置,它运作得很好。
如果您有这个特定的ArcGIS OpenLayers问题,请查看文档: http : //openlayers.org/en/v3.14.2/apidoc/ol.source.ImageWMS.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.